473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

gcc 3.4 template compilation problem

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
2 1293
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ========================================= Here is some program...
15
by: iuweriur | last post by:
A few questions on the curiously recurring template pattern: This page: http://c2.com/cgi/wiki?CuriouslyRecurringTemplate this part: template<typename T> struct ArithmeticType { T operator...
7
by: Patrick Kowalzick | last post by:
Dear all, I just wondered if it is possible to count the number of classes created via a template class at compile time. To show what I mean I post an example, which is not working but carries...
3
by: martin.druon | last post by:
Hi, I created a template class to represent hypermatrix. I would like to add methods where the number of parameters are checked during the compilation time. For example : template <size_t...
4
by: Marcelo | last post by:
Hi everybody, This is my first time with the template class and I have an strange problem. I have spent all the afternoon trying to understand it, but I don't get the problem... I have three...
3
by: Yohan | last post by:
Hello, I have a question concerning the template classes and their parameters. Is it possible to set a condition on the template parameters in a way that could block the compilation if the...
1
by: toton | last post by:
Hi, Is there any tool for template debugging, during compilation process ? Just like gdb or visual studio debugger do it during program execution, a step by step debug for the program, I am...
4
by: carlos.urena.almagro | last post by:
Hello, I'm sorry if this post raises a question with obvious answer, or if it has been already posted and solved. I was wondering about how the compiler (in my case, gcc 4.0.2 on linux)...
8
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will...
1
by: Alex Vinokur | last post by:
Hi, I have compilation problem on SUN CC compiler with template while using option -m64 (64-bit addressing model). No problem while using option -m32 (32-bit addressing model) $ CC -V CC:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.