Joh*_*ell 6 c++ lambda templates traits c++11
我有一个带有成员函数的类模板,该成员函数有一个想要使用类模板参数类型的lambda.它无法在lambda内部编译,但正如预期的那样在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;
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
error C2653: 'reporter' : is not a class or namespace name
Run Code Online (Sandbox Code Playgroud)
为什么我不能在成员函数lambda中访问类模板参数类型?
我需要做什么才能访问成员函数lambda中的类模板参数类型?
使用类型定义:
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);
});
}
};
Run Code Online (Sandbox Code Playgroud)