1

What is the correct process to pass a standard type traits qualified type as an argument to a template class?

I have the following (rather useless but useful to depict my problem) program that qualifies a type via standard type traits before passing to a template class.

#include <iostream>
#include <type_traits>
#include <vector>
template <typename T>
struct is_foo
{
    static bool const value = true;
};
template<
    typename Ty,
    bool foo = is_foo<std::remove_reference<Ty>::type>::value
>
struct bar {
    static bool const value = true;
};
int main() {
    int elem;
    std::cout << bar<decltype(elem)>::value << std::endl;
    return 0;
}

This program compiles fine with VC12 but fails to compile with g++ 4.8 with the following error

prog.cpp:11:51: error: type/value mismatch at argument 1 in template parameter list for ‘template<class T> struct is_foo’
  bool foo = is_foo<std::remove_reference<Ty>::type>::value
                                                   ^
prog.cpp:11:51: error:   expected a type, got ‘std::remove_reference< <template-parameter-1-1> >::type’
prog.cpp: In function ‘int main()’:
prog.cpp:18:33: error: template argument 2 is invalid
  std::cout << bar<decltype(elem)>::value << std::endl;
                                 ^

Any idea what might be wrong with my code?

  • Note If you need to play with the code, here is a version in IDEONE
  • Note Also, it would be quite beneficial if you can refer me back to the standard.
  • Note If I remove the std::remove_reference call, bool foo = is_foo<Ty>::value my code compiles fine. Refer IDEONE
Abhijit
  • 62,056
  • 18
  • 131
  • 204

1 Answers1

3

You are missing a typename (because std::remove_reference<Ty>::type is dependant on a template parameter):

template<
typename Ty,
bool foo = is_foo<typename std::remove_reference<Ty>::type>::value>

                  ^^^^^^^
                    Here

Also see Where and why do I have to put the “template” and “typename” keywords?

Community
  • 1
  • 1
quantdev
  • 23,517
  • 5
  • 55
  • 88