使用 std::async 调用模板化成员函数
Call a templated member function with std::async
是否可以以及如何使用 std::async(最好不使用 std::bind)调用 class 的模板化成员函数?请解释 C++11 或 C++14 标准是否通常允许这样的调用,以及如何使其在 MSVS2013 中工作。
#include <future>
template<typename T> void non_member_template(T val) {}
struct X
{
void member_non_template(int val) {}
template<typename T> void member_template(T val) {}
void call()
{
int val = 123;
std::async(std::launch::async, &non_member_template<int>, val); // ok
std::async(std::launch::async, &X::member_non_template, this, val); // ok
std::async(std::launch::async, &X::member_template<int>, this, val); // error
}
};
int main()
{
X x;
x.call();
}
我以前从未见过代码会破坏编译器,但现在开始:
我在上面的评论中建议与 Axalo 相同,您可以通过强制转换解决编译错误。
#include <future>
template<typename T> void non_member_template( T val ) {}
struct X
{
void member_non_template( int val ) {}
template<typename T> void member_template( T val ) {}
void call()
{
int val = 123;
std::async( std::launch::async, &non_member_template<int>, val ); // ok
std::async( std::launch::async, &X::member_non_template, this, val ); // ok
//std::async( std::launch::async, &X::member_template<int>, this, val ); // error
std::async( std::launch::async, static_cast< void ( X::* )( int )>( &X::member_template<int> ), this, val ); // ok
}
};
int main()
{
X x;
x.call();
}
显然这是 Microsoft Visual C++ 2013 和 2015 预览版中的错误,将在未来版本的 Visual C++ 中修复:Passing member template function to std::async produces compilation error。
所以示例程序是有效的 C++ 代码,如果您需要 Visual C++ 2013 的解决方法,请按照评论中的建议在最后一次调用 std::async
时使用强制转换:
static_cast<void(X::*)(int)>(&X::member_template<int>)
是否可以以及如何使用 std::async(最好不使用 std::bind)调用 class 的模板化成员函数?请解释 C++11 或 C++14 标准是否通常允许这样的调用,以及如何使其在 MSVS2013 中工作。
#include <future>
template<typename T> void non_member_template(T val) {}
struct X
{
void member_non_template(int val) {}
template<typename T> void member_template(T val) {}
void call()
{
int val = 123;
std::async(std::launch::async, &non_member_template<int>, val); // ok
std::async(std::launch::async, &X::member_non_template, this, val); // ok
std::async(std::launch::async, &X::member_template<int>, this, val); // error
}
};
int main()
{
X x;
x.call();
}
我以前从未见过代码会破坏编译器,但现在开始:
我在上面的评论中建议与 Axalo 相同,您可以通过强制转换解决编译错误。
#include <future>
template<typename T> void non_member_template( T val ) {}
struct X
{
void member_non_template( int val ) {}
template<typename T> void member_template( T val ) {}
void call()
{
int val = 123;
std::async( std::launch::async, &non_member_template<int>, val ); // ok
std::async( std::launch::async, &X::member_non_template, this, val ); // ok
//std::async( std::launch::async, &X::member_template<int>, this, val ); // error
std::async( std::launch::async, static_cast< void ( X::* )( int )>( &X::member_template<int> ), this, val ); // ok
}
};
int main()
{
X x;
x.call();
}
显然这是 Microsoft Visual C++ 2013 和 2015 预览版中的错误,将在未来版本的 Visual C++ 中修复:Passing member template function to std::async produces compilation error。
所以示例程序是有效的 C++ 代码,如果您需要 Visual C++ 2013 的解决方法,请按照评论中的建议在最后一次调用 std::async
时使用强制转换:
static_cast<void(X::*)(int)>(&X::member_template<int>)