在类模板中调用模板化函子
Calling a templated functor in a class template
本文关键字:调用 更新时间:2023-10-16
有没有办法调用类模板Foo
的函子operator()( int )
,如下所示(在线版本)
template<typename T>
struct Foo
{
template<typename U>
void operator()( int )
{
}
};
int main(int argc, char *argv[])
{
Foo<char> foo;
foo<bool>( 42 );
}
我在 gcc 4.9.3 中收到错误消息
error: expected primary-expression before ‘bool’
foo<bool>( 42 );
如果成员函数不是函子并且前缀为 ::
、.
或 ->
,我会在函子前面加上 template
。如果没有一些帮助,编译器就不知道如何解析这个表达式;作为 foo<int>
类型的匿名对象的函子或实例化。
它可以与;
foo.operator()<bool>( 42 );
运算符最适合使用推导的模板参数类型。
您没有提供足够的详细信息来说明使用它的上下文,作为您可以考虑的替代方案;
- 使调用运算符成为成员函数,从而允许显式模板类型参数
- 标签调度机制
- 接受类型
U
作为参数
例如;
template<typename U>
void operator()( int, U&& /*initial*/ )
{
// use the initial value of U
}
// called as...
foo(42, true); // U is bool in your example
或者只是成员函数;
template<typename U>
void my_func( int )
{
}
// called as...
foo.my_fun<bool>( 42 );
是的,但它很丑陋:
foo.operator()<bool>( 42 );
不幸的是,您需要为此使用foo.operator()<bool>(42);
。 foo<bool>
对 C++14 变量模板等内容有效,而不是模板调用运算符。
您可以做的是标记类型并将其作为参数传递给调用运算符以推断正确的类型:
//tag
template <typename T>
struct type{};
template<typename T>
struct Foo
{
template<typename U>
//deduction on the tagged type
void operator()( type<U>, int )
{
}
};
int main(int argc, char *argv[])
{
Foo<char> foo;
foo( type<bool>{}, 42 );
// ^^^^^^^^^^^^ pass tag
}
您可以使用标签的 C++14 个变量模板来使其更好一些:
template <typename T>
struct type_t{};
//variable template for nicer usage
template <typename T>
type_t<T> type;
template<typename T>
struct Foo
{
template<typename U>
void operator()( type_t<U>, int )
{
}
};
int main(int argc, char *argv[])
{
Foo<char> foo;
foo( type<bool>, 42 );
//don't need {}^
}
可悲的是,如果函子本身是一个模板,语法会变得更加复杂。以下操作将失败:
template <typename Functor, typename ... Args>
void Foo (Functor & f)
{
f.operator()<Args...>();
}
struct MyFunctor
{
template <typename ...>
void operator () () {}
};
int main ()
{
MyFunctor f;
Foo(f);
}
相反,我们必须写:
f.template operator()<Args...>();
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 调用'begin(int [n])'没有匹配函数
- 什么时候调用析构函数
- 如何用参数值调用函数(仅在运行时已知)
- std::cout.imbue()多重调用
- 函数何时会在c++中包含stack_Unwind_Resume调用