Search code examples
c++conditional-statementsmetaprogrammingvalue-typedecltype

Why Can't I Get the bool Value from a value_type Returned by decltype?


This appears to be a problem. This code runs fine in gcc but fails to compile in Visual Studio:

#include <iostream>
#include <type_traits>
#include <typeinfo>

using namespace std;

true_type foo();

template <typename T>
struct bar{
    using def = conditional_t<decltype(foo())::value, char, void>;
};

int main() {
     cout << typeid(bar<int>::def).name() << endl;

     cout << decltype(foo())::value << endl;
}

The error given is:

error C2146: syntax error: missing > before identifier value

Live Example

Is there a bug fix for this or a workaround?


Solution

  • I'm not sure if it can help you, but I am able to work around the problem like this:

    First define:

    template <typename I> using id = I;
    

    Then replace every instance of decltype(foo())::value with

    id<decltype(foo())>::value
    

    Alternately, you could use std::common_type_t the same way:

    std::common_type_t<foo()>::value
    

    Or, my psychic powers predict you might just want to define a separate type for decltype<foo()>, for convenience:

    using id = decltype(foo());

    then replace all instances of decltype(foo())::value with id::value.