473,779 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me 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<typena me 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 1330
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<typena me 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<typena me 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
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 that the value passed would be added to the mapped_value of each pair in the map.But the program fails to compile. Can you kindly point out my error. The source code is given below.
13
2828
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 { }; template <typename Base> class Tpl : protected Base {
0
1634
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 { template <class S>
12
2625
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 an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
2
1852
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: template<typename InputIterator> Foo(InputIterator i); Foo(const Bar & b);
1
2343
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
2568
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 another template like 'pair', because I might have a bunch of useful
4
3356
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
2623
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 when you give it 2 or more words, but when there's only 1 word the results vary depending on whether it's on Windows or Linux: under MSVC it displays no output (as it should); under gcc/Linux it instead gives "Segmentation fault". Any ideas...
9
2065
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
0
9636
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9930
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.