Re: problem with partial ordering of classes
On 25 Jul., 17:18, Rodolfo Lima <rodo...@rodsoft.org> wrote:
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?
This time it's you ;-)
The problem is that in the attempt to define the partial
specialization
template<class T, int ID>
struct foo<bar<T>,ID, typename enable_if<ID!=0>::type>;
you are running into a non-deducible context for the last
argument. Unfortunately the standard does not require this
attempt to be ill-formed. There is a core issue concerning
this situation, see:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#549
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]