2

I asked a question here, C++ template argument with expression, and got an answer but now I want to move onto something like this:

#include <complex>
#include <type_traits>

using namespace std;

template<class T, class U>
complex< conditional<(sizeof( T ) > sizeof( U )), T, U>::type > my_method(T x, U y)  { }


This gives me the errors:

main.cpp:10:65: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp> struct std::complex’
main.cpp:10:65: error:   expected a type, got ‘std::conditional<(sizeof (T) > sizeof (U)), int, float>::type’


I have tried adding the key word typename and class before T and U, but that changes the error too:

main.cpp:37:1: error: wrong number of template arguments (1, should be 3)
/usr/include/c++/4.7/type_traits:77:12: error: provided for ‘template<bool <anonymous>, class, class> struct std::conditional’
main.cpp:10:10: error: template argument 1 is invalid
main.cpp:10:1: error: expected unqualified-id at end of input


I am using g++ with -std=c++11 as a compiler option on linux. Is there anyway to do what I am trying to do? Thanks.

Community
  • 1
  • 1
Toby
  • 286
  • 3
  • 8
  • 2
    try `typename conditional<....>::type`. `typename` is not needed in the answer you've linked to because there the types are known, and so the `conditional::type` nested name is not dependent. Here it depends on `T` and `U`. Read [this](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) for details. – Praetorian Aug 13 '13 at 22:20

1 Answers1

2

As your compiler says:

main.cpp:10:65: error:   expected a type, got ‘std::conditional<(sizeof (T) > sizeof (U)), int, float>::type’

There is a typename missing before std::conditional<....

Patrick Sweigl
  • 373
  • 3
  • 18