Access to class template parameter type inside member function with lambda error

I have a class template with a member function that has lambda that wants to use the class template parameter type. It does not compile inside lambda, but succeeds, as expected, outside of lambda.

struct wcout_reporter { static void report(const std::wstring& output) { std::wcout << output << std::endl; } }; template <typename reporter = wcout_reporter> class agency { public: void report_all() { reporter::report(L"dummy"); // Compiles. std::for_each(reports_.begin(), reports_.end(), [this](const std::wstring& r) { reporter::report(r); // Fails to compile. }); } private: std::vector<std::wstring> reports_; }; int wmain(int /*argc*/, wchar_t* /*argv*/[]) { agency<>().report_all(); return 0; } 

Compilation Error:

 error C2653: 'reporter' : is not a class or namespace name 

Why can't I access the class template parameter type inside the lambda member function?

What do I need to do to access the template template parameter type inside the lambda member function?

+6
source share
2 answers

Use typedef:

 template <typename reporter = wcout_reporter> class agency { typedef reporter _myreporter; public: void report_all() { reporter::report(L"dummy"); // Compiles. std::for_each(reports_.begin(), reports_.end(), [this](const std::wstring& r) { // Take it agency<>::_myreporter::report(r); }); } }; 
+2
source

This should compile OK as is. It looks like your compiler has an error in the rules for finding names in lambda. You can try adding typedef for reporter inside report_all .

+4
source

Source: https://habr.com/ru/post/891126/


All Articles