473,491 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Re: Inconsistent template behavior; standard-conforming, UB, or gccbug?

Paavo Helde wrote:
So you have to declare all bar() overloads before your template
definitions, and I bet the visible declarations in both files have to be
the same to avoid UB.
So which compiler is correct, gcc (which only requires bar() to be
declared at the point of insantiation of foo()) or comeau?

What happens if you just move the bar() function to be before the
foo() function in both source files? (It's not like this was critical
from the point of view of my original post.)
Jun 27 '08 #1
2 1617
On May 16, 10:07 am, Juha Nieminen <nos...@thanks.invalidwrote:
Paavo Helde wrote:
So you have to declare all bar() overloads before your template
definitions, and I bet the visible declarations in both files have to be
the same to avoid UB.
So which compiler is correct, gcc (which only requires bar() to be
declared at the point of insantiation of foo()) or comeau?
Comeau. It's more subtle that one might suspect from Paavo's
posting, but this is an error in g++ (at least in version
4.1.0).
What happens if you just move the bar() function to be before
the foo() function in both source files? (It's not like this
was critical from the point of view of my original post.)
It's undefined behavior, and at least with the version of g++ I
have handy, and my usual compile flags, depends on the
optimization level. In your code, it's clear that:

1. you have two different functions bar,
2. the instantiaion foo<inttakes place at several different
places, and
3. the name bar in this instantiation is bound to a different
function, depending on the point of instantiation.

Point 3 leads to undefined behavior, according to the last
paragraph of §14.6.4.1:

A specialization for a function template, a member
function template, or of a member function or static
data member of a class template may have multiple points
of instantiations within a translation unit. A
specialization for a class template has at most one
point of instantiation within a translation unit. A
specialization for any template may have points of
instantiation in multiple translation units. If two
different points of instantiation give a template
specialization different meanings according to the one
definition rule (3.2), the program is ill-formed, no
diagnostic required.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #2
James Kanze wrote:
Point 3 leads to undefined behavior, according to the last
paragraph of §14.6.4.1:

A specialization for a function template, a member
function template, or of a member function or static
data member of a class template may have multiple points
of instantiations within a translation unit. A
specialization for a class template has at most one
point of instantiation within a translation unit. A
specialization for any template may have points of
instantiation in multiple translation units. If two
different points of instantiation give a template
specialization different meanings according to the one
definition rule (3.2), the program is ill-formed, no
diagnostic required.
Thanks. That was helpful information.
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
371
by: Senthilvel | last post by:
Hi , I am trying out a few templates and i got stuck in template specialization. The normal template Add(just like a plus of the fubnctional) works fine. But i wanted to specialize it for a map so...
13
2791
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
0
1622
by: ckhoge | last post by:
Hi, Consider this code fragment, based on the article "Using Chains to Free Library Code" from the July 2005 issue of C/C++ Users Journal: -- begin listing -- template <class T> struct Foo...
12
2591
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
2
1839
by: Glenn G. Chappell | last post by:
I am trying to write two constructors for the same class. One takes an iterator and so is a template. The other takes a particular type by reference to const. class Foo { public:...
1
2325
by: Peter Knörrich | last post by:
Hello, I've found another inconsistency, and looking through the list archives I can find mentions of funky stuff like print float('inf') giving Infanity
19
2533
by: n.torrey.pines | last post by:
I have the following tree definition: template<typename T> struct tree { T first; vector<tree<T second; // branches }; which compiles successfully. What I'd like to do though is to use...
4
3337
by: stinos | last post by:
Hi All! suppose a class having a function for outputting data somehow, class X { template< class tType > void Output( const tType& arg ) { //default ToString handles integers/doubles
20
2577
by: Francine.Neary | last post by:
I am learning C, having fun with strings & pointers at the moment! The following program is my solution to an exercise to take an input, strip the first word, and output the rest. It works fine...
9
2043
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
0
7115
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
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6858
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
7360
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
5451
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,...
1
4881
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...
0
1392
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.