访问具有lambda的成员函数内部的类模板参数类型失败
Accessing a class template parameter type inside a member function with a lambda fails
我有一个类模板,它有一个成员函数,该函数有一个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;
}
编译错误:
error C2653: 'reporter' : is not a class or namespace name
为什么我不能访问成员函数lambda内部的类模板参数类型?
我需要做些什么才能访问成员函数lambda中的类模板参数类型?
这应该可以按原样编译。您的编译器在lambda中的名称查找规则中似乎存在错误。您可以尝试在report_all
中为reporter
添加一个typedef
。
使用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);
});
}
};
相关文章:
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- variadic模板中的模板参数推导失败
- 当给定默认值时,为什么此模板参数推导失败
- 模板参数推导失败,函数参数/参数不匹配
- 为什么模板参数推导失败?
- 嵌套参数包扩展失败
- 模板参数的 C++ 自动模板推导失败
- 调试模板时出现问题.专门针对 Linux GCC 7、GCC 6、GCC 5、GCC 4.9 错误构建失败:模板参数 1
- 使用显式模板参数列表和 [temp.arg.explicit]/3 的函数调用的演绎失败
- 失败,出现错误 87:参数不正确.如何判断哪个参数不正确?
- 在 g++ 6.2.1 中将参数包转发到 constructor() 失败
- 当变量和参数名称匹配时,移动语义构造失败
- 作为模板参数的成员函数指针在继承的成员函数上失败,如何以及为什么?
- 模板参数推导/替换失败,lambda作为函数指针
- 为什么在 std::transform 中使用函数对象参数失败并且需要 lambda 表达式
- C++模板参数失败,不是从错误派生的
- CreateThread参数失败
- 在CreateProcess中传递命令行参数失败