我正在尝试从类型包中实例化参数。这允许我迭代类型(没有涉及真正的参数,但我默认实例化它们以通过将伪args传递给另一个模板函数来迭代类型)
template<typename T>
inline int iterate(T&& dummy) { return T::bar(); }
但问题在于,实例化虚拟参数。 它对基类型和指针类型有预期效果,但不适用于标准类型(我用std :: string和std :: unique_ptr测试它)
这在Visual Studio 2013中无法编译:
#include <string>
class A{};
template<typename... Args>
inline void pass(Args&&...) {}
template <class... Args>
void test() { pass(Args()...); }
int main() {
test<int, int, float, A*, A*>(); // this is fine
test<int, int, std::string>(); // error C2062:
// type 'std::string' unexpected
test<int, int, float, A*, A*, std::string>(); // error C2062:
// type 'std::string' unexpected
}
有没有人有任何指针,原因是什么?它是编译器特定的(vs120)吗?
然而,第一个有问题的电话会扩展为:
pass( int(), int(), std::string() );
哪个应该没问题,不是吗?
所以我似乎遇到了与此处链接相同的编译器问题: (Default) construct an object for every variadic type
我使用了相同的解决方法,现在编译得很好:
template<typename T> using msvc_identity_fix = T;
template<typename... Args> inline void pass(Args&&...) {}
template <class... Args>
void test() { pass(msvc_identity_fix<Args>()...); }