473,405 Members | 2,176 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.

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

Juha Nieminen wrote:
Let's assume we have one .cc file which contains the following:

// ----------- file 1 -----------
#include <iostream>

template<typename T>
void foo(T t) { bar(t); }

void bar(int i) { std::cout << "int: " << i << std::endl; }

void anotherFunc();

int main() { foo(5); anotherFunc(); }
// ------------------------------

And another file which contains the following:

// ----------- file 2 -----------
#include <iostream>

template<typename T>
void foo(T t) { bar(t); }

void bar(long i) { std::cout << "long: " << i << std::endl; }

void anotherFunc() { foo(7); }
// ------------------------------
[surprising behavior snipped]
So my question is: Is this behavior (ie. the inconsistent behavior of
these template functions depending on whether they are inlined or not)
standard-conforming, is it Undefined Behavior(TM), or is it a bug in gcc?
I think your code violates the One-Definition-Rule. The template function
foo() is defined in two significantly different ways in the two files. As
far as I know, no diagnostic is required for such errors.
[snip]
Best

Kai-Uwe Bux

Jun 27 '08 #1
6 1305
Kai-Uwe Bux wrote:
I think your code violates the One-Definition-Rule. The template function
foo() is defined in two significantly different ways in the two files. As
far as I know, no diagnostic is required for such errors.
The inconsistent behavior happens also if the two foo() functions are
identical (which is exactly what happens if the foo() funtion had been
implemented in a header file which is then included in both source
files, as is usually the case with template functions). It's not only
the differently-implemented foo() functions which are the issue here.
Jun 27 '08 #2
Juha Nieminen wrote:
Kai-Uwe Bux wrote:
>I think your code violates the One-Definition-Rule. The template function
foo() is defined in two significantly different ways in the two files. As
far as I know, no diagnostic is required for such errors.

The inconsistent behavior happens also if the two foo() functions are
identical (which is exactly what happens if the foo() funtion had been
implemented in a header file which is then included in both source
files, as is usually the case with template functions). It's not only
the differently-implemented foo() functions which are the issue here.
Could you post the code you are looking at.
Best

Kai-Uwe Bux
Jun 27 '08 #3
Kai-Uwe Bux wrote:
Could you post the code you are looking at.
I already posted it in my original post. But fine, to make it clearer,
here's the code divided into a header file and two .cc files:

//------------ foo.hh --------------
#include <iostream>

template<typename T>
void foo(T t)
{
// Uncomment these two lines for the other behavior:
//static int count = 0;
//std::cout << "count: " << ++count << std::endl;

bar(t);
}
//----------------------------------

// ----------- main.cc -------------
#include "foo.hh"

void bar(int i) { std::cout << "int: " << i << std::endl; }

void b();

int main() { foo(5); b(); }
//----------------------------------

// ----------- b.cc ----------------
#include "foo.hh"

void bar(long i) { std::cout << "long: " << i << std::endl; }

void b() { foo(7); }
//----------------------------------

I'm using gcc 4.1.2. Running it like it is above it prints:

int: 5
long: 7

Uncommenting the two lines in the header makes it print:

count: 1
int: 5
count: 2
int: 7
Jun 27 '08 #4
Juha Nieminen wrote:
Kai-Uwe Bux wrote:
>Could you post the code you are looking at.

I already posted it in my original post. But fine, to make it clearer,
here's the code divided into a header file and two .cc files:

//------------ foo.hh --------------
#include <iostream>

template<typename T>
void foo(T t)
{
// Uncomment these two lines for the other behavior:
//static int count = 0;
//std::cout << "count: " << ++count << std::endl;

bar(t);
}
//----------------------------------

// ----------- main.cc -------------
#include "foo.hh"

void bar(int i) { std::cout << "int: " << i << std::endl; }

void b();

int main() { foo(5); b(); }
//----------------------------------

// ----------- b.cc ----------------
#include "foo.hh"

void bar(long i) { std::cout << "long: " << i << std::endl; }

void b() { foo(7); }
//----------------------------------

I'm using gcc 4.1.2. Running it like it is above it prints:

int: 5
long: 7

Uncommenting the two lines in the header makes it print:

count: 1
int: 5
count: 2
int: 7

Ok. What about [14.6.4.1/7]:

... 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.

I think, this applies to foo<int>.
Best

Kai-Uwe Bux
Jun 27 '08 #5
Kai-Uwe Bux wrote:
Ok. What about [14.6.4.1/7]:

... 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.

I think, this applies to foo<int>.
Thus the answer to the question I posed in the thread subject is
undefined behavior?

So basically this means the compiler is allowed to do whatever it
wants, and thus gcc is not really misbehaving in this case?

Thanks. That was informative.
Jun 27 '08 #6
Juha Nieminen wrote:
Kai-Uwe Bux wrote:
>I think your code violates the One-Definition-Rule. The template function
foo() is defined in two significantly different ways in the two files. As
far as I know, no diagnostic is required for such errors.

The inconsistent behavior happens also if the two foo() functions are
identical (which is exactly what happens if the foo() funtion had been
implemented in a header file which is then included in both source
files, as is usually the case with template functions). It's not only
the differently-implemented foo() functions which are the issue here.
If you have a single foo definition and you make it available all bar
overloads at the point of the definition then you should get same
behaviour.

--
Dizzy

Jun 27 '08 #7

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

Similar topics

5
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
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
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
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
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
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
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
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
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
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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.