Re: template parameter not used in partial specialization
Am 25.02.2013 21:44, schrieb Walter Mascarenhas:
The code below does not compile in g++ 4.7.2: it says
that the template parameter S is not used in Foo's
specialization below. Is g++ correct on this? If so,
how would I specialize the struct Foo for
T = std::vector<S>::iterator in a general way?
#include <vector>
template <typename T>
struct Foo
{
};
template <typename S>
struct Foo< typename std::vector<S>::iterator >
{
};
Yes, the code is ill-formed, no diagnostic required. Reason is that the
partial specialization can never be deduced. This is similar to the
situation of the following function template:
#include <vector>
template<class S>
void foo(typename std::vector<S>::iterator) {}
int main() {
foo(std::vector<int>().begin());
}
Here we have a similar non-deduced context as in the case of your
partial specialization.
Think a second time of what you are trying to realize here: The type
std::vector<S>::iterator has not necessarily an exact inverse relation
to the template parameter S of std::vector<S>. So it is not really clear
to me what you are trying to achieve. E.g. if std::vector<S>::iterator
would be S*, how should the compiler know that if you provide int* that
the partial specialization should be taken? Type 'int*' may be an
associated type for several types. There is nothing special about it
being (just by chance) identical to std::vector<int>::iterator.
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! ]