467,925 Members | 1,812 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,925 developers. It's quick & easy.

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
  • viewed: 1088
Share:
6 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Senthilvel | last post: by
reply views Thread by ckhoge | last post: by
12 posts views Thread by mlimber | last post: by
2 posts views Thread by Glenn G. Chappell | last post: by
1 post views Thread by Peter Knörrich | last post: by
19 posts views Thread by n.torrey.pines | last post: by
4 posts views Thread by stinos | last post: by
20 posts views Thread by Francine.Neary | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.