R
Rodolfo Lima
Hi, I'm having some problems with the following code, gcc-4.5 refuses
to compile it:
#include <iostream>
template <bool B, class V=void> struct enable_if {};
template <class V> struct enable_if<true,V> { typedef V type; };
template <class T>
struct bar {};
template <class T, int ID, class V=void>
struct foo;
template<template<class> class V, class T, int ID>
struct foo<V<T>, ID>
{
static const int value = 1;
};
template<class T, int ID>
struct foo<bar<T>,ID, typename enable_if<ID!=0>::type>
{
static const int value = 2;
};
int main()
{
std::cout << foo<bar<int>,1>::value << std::endl;
}
//------------------------------------------
The error message:
teste.cpp: In function ‘int main()’:
teste.cpp:26:30: error: ambiguous class template instantiation for
‘struct foo<bar<int>, 1>’
teste.cpp:14:1: error: candidates are: struct foo<V<T>, ID>
teste.cpp:20:1: error: struct foo<bar<T>, ID, typename
enable_if<(ID != 0)>::type>
teste.cpp:26:15: error: incomplete type ‘foo<bar<int>, 1>’ used in
nested name specifier
According to partial ordering rules, struct foo<bar<T>,ID, typename
enable_if<ID!=0>::type> is more specialized than struct foo<V<T>, ID>,
isn't it? So it should be used, instead of giving an ambiguity error.
Am I wrong or is the compiler?
Regards,
Rodolfo Lima
to compile it:
#include <iostream>
template <bool B, class V=void> struct enable_if {};
template <class V> struct enable_if<true,V> { typedef V type; };
template <class T>
struct bar {};
template <class T, int ID, class V=void>
struct foo;
template<template<class> class V, class T, int ID>
struct foo<V<T>, ID>
{
static const int value = 1;
};
template<class T, int ID>
struct foo<bar<T>,ID, typename enable_if<ID!=0>::type>
{
static const int value = 2;
};
int main()
{
std::cout << foo<bar<int>,1>::value << std::endl;
}
//------------------------------------------
The error message:
teste.cpp: In function ‘int main()’:
teste.cpp:26:30: error: ambiguous class template instantiation for
‘struct foo<bar<int>, 1>’
teste.cpp:14:1: error: candidates are: struct foo<V<T>, ID>
teste.cpp:20:1: error: struct foo<bar<T>, ID, typename
enable_if<(ID != 0)>::type>
teste.cpp:26:15: error: incomplete type ‘foo<bar<int>, 1>’ used in
nested name specifier
According to partial ordering rules, struct foo<bar<T>,ID, typename
enable_if<ID!=0>::type> is more specialized than struct foo<V<T>, ID>,
isn't it? So it should be used, instead of giving an ambiguity error.
Am I wrong or is the compiler?
Regards,
Rodolfo Lima