By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,482 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

gcc 3.4 template compilation problem

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
bi*********@hotmail.com wrote:
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


Jul 23 '05 #2

P: n/a

bigfacew...@hotmail.com wrote:
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-

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.