Alexander
Alexander

Reputation: 758

compilation error under gcc

i got a compilation error grom gcc (versions less than 6). clang and vc2013/2015 and gcc6 do not complain about this code. So i think this it is a bug of gcc 4.x-5.x. is there any workaround to make this code compile on 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;
}

Upvotes: 0

Views: 169

Answers (1)

ForEveR
ForEveR

Reputation: 55887

Looks like bug in gcc, but you can just write function, that returns type_index as a workaround.

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)...);
}

or you can just not to use auto

template <typename... ParamTypes> 
void CreateObject(ParamTypes... args)
{
    tuple<type_index, type_index, type_index> types =
    make_tuple(typeindex(typeid(args))...);
}

Upvotes: 2

Related Questions