gcc 3.4 template compilation problem

B

bigfaceworm

I had what I thought was proper SFINAE code. Two declarations of a
function, one more general, one more specific. The code compiles just
fine on gcc 3.3, but fails on 3.4 and 3.4.2. What is wrong with my
code?

My understanding was that the second routine would be called because it
is a better match, and the first one would not be instantiated because
there are no matches. Note: my real example involves constructors to a
template class that I only want to be usable when the class is
specialized (and provides a proper implementation).

The error is:

/tmp/specialized.cpp: In function `void f(T)':
/tmp/specialized.cpp:4: error: size of array `a' is negative


The code is:
------------------------------------------------------------------------
template <typename T>
void f(T t)
{
int a[-1];
}

template <>
void f(int *)
{
;
}

int
main()
{
int a;
f(&a);
return 0;
}
 
R

Rolf Magnus

I had what I thought was proper SFINAE code. Two declarations of a
function, one more general, one more specific. The code compiles just
fine on gcc 3.3, but fails on 3.4 and 3.4.2. What is wrong with my
code?

My understanding was that the second routine would be called because it
is a better match, and the first one would not be instantiated because
there are no matches.

It's not really about matching. It's not an overloaded function. Rather, the
second is a specialization of the template, used whenever the template is
to be instantiated for the template parameters specified in this
specialization.
Note: my real example involves constructors to a
template class that I only want to be usable when the class is
specialized (and provides a proper implementation).

The error is:

/tmp/specialized.cpp: In function `void f(T)':
/tmp/specialized.cpp:4: error: size of array `a' is negative

Well, the template isn't instantiated. If it were, g++ would also include a
reference to the line that it's instantiated from.
The code is:
------------------------------------------------------------------------
template <typename T>
void f(T t)
{
int a[-1];
}

template <>
void f(int *)
{
;
}

int
main()
{
int a;
f(&a);
return 0;
}
------------------------------------------------------------------------
Thanks,


BFW
 
S

shez

I had what I thought was proper SFINAE code. Two declarations of a
function, one more general, one more specific. The code compiles just
fine on gcc 3.3, but fails on 3.4 and 3.4.2. What is wrong with my
code?

My understanding was that the second routine would be called because it
is a better match, and the first one would not be instantiated because
there are no matches. Note: my real example involves constructors to a
template class that I only want to be usable when the class is
specialized (and provides a proper implementation).

The error is:

/tmp/specialized.cpp: In function `void f(T)':
/tmp/specialized.cpp:4: error: size of array `a' is negative


The code is:
------------------------------------------------------------------------
template <typename T>
void f(T t)
{
int a[-1];
}

template <>
void f(int *)
{
;
}

int
main()
{
int a;
f(&a);
return 0;
}
------------------------------------------------------------------------
Thanks,


BFW

The compiler is not "calling" the first template function. But it is
allowed to do *some* checks that are not related to 'T'. And 'int
a[-1]' is not related to T, so the compiler issues an "early" error,
even though the function is not actually used.

To solve your problem (re ctors), simply make them private in your
"generic" class.

-shez-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top