how to create the body of a thread and use it in google test

0

First, forgive if the title is not the most appropriate.

I am trying to write a google test, which launches several threads, as a testlogging. This would be the function that the threads would execute.

    void log(std::vector<int>& A, severity_t severity){
         for(size_t scanA(0);scanA!=A.size();++scanA){
             switch(severity){
                    case severity_t::debug:
                         //do something
                         break;
                    case severity_t::info:
                         //do something
                         break;
                    default:
                         break;
             }
          }
    }

The test is written as:

TEST(testLog, testA){
    //Inicializo A
    //Hago un thread para cada severity:
        std::thread logDebug(std::bind(&log,A,severity_t::debug));
        std::thread logInfo(std::bind(&log,A,severity_t::info));
        //...
    //Y hago el join para cada uno
        logDebug.join();
        logInfo.join();
        //...
    //Hago lo necesario para obtener resultados. y termino el test.
    }

I am compiling in a redHat with gcc 4.4.7 and with -std = c ++ 0x by obligation. And the result of the compilation is: error: no matching function for call to 'bind (< unresolved overloaded function type & std :: vector

asked by user2357667 19.09.2017 в 18:46
source

1 answer

0

Problem.

The error is clear and crystal clear, but maybe you're not familiar with being in English, let me translate it:

  

error: no function matches to call 'bind (, std :: vector & severity_t)'

The error is telling you that there is more than one function with the same name (function overloaded) and with only the name does not know which one to use. You can see the same error with this simplified example:

void log(int){}
void log(double){}

int main( void )
{
    auto b1 = std::bind(log, 1); // < tipo de función sobrecargada no resuelto >

    return 0;
}

Solution.

Since the name log is ambiguous due to overloads, you should tell the compiler what version of the log function you want to use, you can use a conversion for it:

void log(int){}
void log(double){}

int main( void )
{
    using log_int = void(int); // Alias al tipo 'función que recibe int'

    auto b1 = std::bind(static_cast<log_int *>(log), 1);
    //                  ^^^^^^^^^^^^^^^^^^^^^^
    //          queremos usar la version que recibe int

    return 0;
}

Note.

In your code you have only put a version of log , so I can not help you to deduce the type you want to disambiguate, but it could possibly be something like this:

using log_t = void(std::vector<int>&, severity_t);

std::thread logDebug(std::bind(static_cast<log_t *>(log),A,severity_t::debug));
std::thread logInfo(std::bind(static_cast<log_t *>(log),A,severity_t::info));
    
answered by 20.09.2017 в 08:55