cppreference重载"技巧"其中每个变体可以通过模板operator()
重载访问,不会使用Visual C ++编译器进行编译。可以找到代码段here,并在使用clang或gcc编译时执行正常。
但是,这不能用MSVC(see on godbolt)编译:
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
它会抛出各种错误并最终失败:
warning C4346: 'Ts::()': dependent name is not a type
note: prefix with 'typename' to indicate a type
note: see reference to class template instantiation 'overloaded<Ts...>' being compiled
error C2143: syntax error: missing ';' before '...'
error C2059: syntax error: '...'
error C2238: unexpected token(s) preceding ';'
error C2988: unrecognizable template declaration/definition
error C2143: syntax error: missing ')' before '...'
error C2143: syntax error: missing ';' before '...'
error C2365: 'Ts': redefinition; previous definition was 'template parameter'
note: see declaration of 'Ts'
error C2059: syntax error: ')'
error C2059: syntax error: '->'
error C2065: 'Ts': undeclared identifier
error C3544: 'Ts': parameter pack expects a type template argument
还有其他选择吗?我错过了编译器的选项吗?
答案 0 :(得分:1)
template<class...Ts>
struct overloaded_t {};
template<class T0>
struct overloaded_t<T0>:T0 {
using T0::operator();
overloaded_t(T0 t0):T0(std::move(t0)) {}
};
template<class T0, class T1, class...Ts>
struct overloaded_t<T0, T1, Ts...>:T0, overloaded_t<T1, Ts...> {
using T0::operator();
using overloaded_t<T1, Ts...>::operator();
overloaded_t(T0 t0, T1 t1, Ts... ts):
T0(std::move(t0)),
overloaded_t<T1, Ts...>(std::move(t1), std::move(ts)...)
{}
};
template<class...Ts>
overloaded_t<Ts...> overloaded(Ts...ts){ return {std::move(ts)...}; }
或升级到最新的编译器。