0

I'm trying to compile the following C++14 code with GCC 5.4 (online example):

template<typename T>
struct traits {
    template<typename X>
    static constexpr bool vect = true;
};

template<typename T1, typename T2>
constexpr bool all_vect = traits<T1>::template vect<T2>;

bool something() {
    return all_vect<void, double>;
}

But I got the following errors:

<source>: In instantiation of 'constexpr const bool all_vect<void, double>':
<source>:11:12:   required from here
<source>:8:16: error: 'template<class X> constexpr const bool traits<void>::vect<X>' is not a function template
 constexpr bool all_vect = traits<T1>::template vect<T2>;
                ^
<source>:8:16: error: 'vect<T2>' is not a member of 'traits<void>'
Compiler exited with result code 1

While I have no problems in GCC 6.1 or more or in clang 3.9 or more. But it's the same for all versions of GCC5 i've tried.

I do not find the reason for this ? Normally, GCC5 should be C++14 feature complete.

Is there a simple workaround for this problem in GCC5 still using variable templates ? I'd rather not go back to using simple traits since I'm converting all my traits to using variable templates.

0

1 Answer 1

1

It is a bug fixed in gcc6 as shown in the dupe.

It looks like there is no workaround while keeping the template variables.

For a workaround that shies away from variable templates you can use good old static non-templated variable:

template<typename T>
struct traits {

    template<typename X>
    struct Is_vect
    {
        static constexpr bool value = true;
    };
};

template<typename T1, typename T2>
struct Are_all_vect
{
    static constexpr bool value = traits<T1>::template Is_vect<T2>::value;
};


bool something() {
    return Are_all_vect<void, double>::value;
}

or constexpr templated functions:

template<typename T>
struct traits {
    template<typename X>
    static constexpr bool vect() { return true; }
};

template<typename T1, typename T2>
constexpr bool all_vect() { return traits<T1>::template vect<T2>(); }

bool something() {
    return all_vect<void, double>();
}
2
  • Thanks for answer. I was actually looking for a simpler workaround still using variable templates. I'd rather not go back to using simple traits since I'm converting all my traits to using variable templates. Aug 10, 2017 at 8:05
  • 1
    @BaptisteWicht it looks like your only option is a newer version of the compiler with the bug fixed or go back to the old traits, or you can have constexpr functions.
    – bolov
    Aug 10, 2017 at 8:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.