What's wrong with the line of code with compile errors (in comment), in the code below? I thought it is supposed to call the TestMe constructor with the const std::string, then call operator() on it. But it looks like it is trying to construct TestMe with variable a.
#include <iostream>
namespace
{
class TestMe
{
public:
TestMe() {}
TestMe( const std::string& me ) : me_( me ) {}
bool operator()() const
{
std::cout << "calling operator()" << std::endl;
return true;
}
private:
const std::string me_;
};
}
int main()
{
const std::string a("a");
//
// construct an instance of TestMe and call its operator()
//
TestMe()(); // OK
TestMe( "abc" )(); // OK
TestMe tm( a ); // OK
TestMe( a )(); // compile error: what's wrong with this?
TestMe( { a } )(); // OK
return 0;
}
Compile error:
../src/main.cpp: In function ‘int main()’:
../src/main.cpp:44:14: error: ‘{anonymous}::TestMe a()’ redeclared as different kind of symbol
TestMe( a )(); // compile error
^
../src/main.cpp:35:20: note: previous declaration ‘const string a’
const std::string a("a");
Aucun commentaire:
Enregistrer un commentaire