[英]compilation error under gcc
我收到了grom gcc(版本小于6)的编译错误。 clang和vc2013 / 2015和gcc6不会抱怨此代码。 所以我认为这是gcc 4.x-5.x的错误。 有什么变通办法可以使此代码在gcc 4.x-5.x上编译?
#include <tuple>
#include <typeinfo>
#include <typeindex>
using namespace std;
template <typename... ParamTypes>
void CreateObject(ParamTypes... args)
{
// error: conversion from 'std::tuple<std::type_index, std::type_index, std::type_index>' to non-scalar type 'std::tuple<std::type_index>' requested
auto types = make_tuple(type_index(typeid(args))...);
}
int main()
{
CreateObject(1, "2", 1.1f);
return 0;
}
看起来像gcc中的bug,但您只能编写函数,该函数返回type_index
作为解决方法。
template<typename T>
type_index get_index(const T& v)
{
return type_index(typeid(v));
}
template <typename... ParamTypes>
void CreateObject(ParamTypes... args)
{
auto types = make_tuple(get_index(args)...);
}
否则你就不能使用auto
template <typename... ParamTypes>
void CreateObject(ParamTypes... args)
{
tuple<type_index, type_index, type_index> types =
make_tuple(typeindex(typeid(args))...);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.