I have these files :-
1.h :-
#include <iostream>
using namespace std;
template <typename A>
void f() {
cout<<"generic\n";
}
1.cpp :-
#include "1.h"
template <>
void f<int> () {
cout<<"for ints only\n";
}
main.cpp :-
#include "1.h"
int main() {
f<int>();
return 0;
}
Now, I compile and run these with g++ like this :-
g++ -c 1.cpp -o 1.o
g++ main.cpp 1.o
./a.out
And I get :-
for ints only
On the other hand, I compile it with icpc like this :-
icpc -c 1.cpp -o 1.o
icpc main.cpp 1.o
./a.out
And I get :-
generic
What does the C++ standard say about this? Is any one compiler "right" and the other "wrong" or is the standard ambiguous on this issue and both are "right" ?
Your program exhibits undefined behavior. The specialization must be declared in every translation unit in which it is used, per C++11 ยง14.7.3/6:
If a template, a member template or a member of a class template is explicitly specialized then that specialization shall be declared before the first use of that specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required.
Related
The following is a simple example for separate compilation:
// mod.cpp
#include <cstdio>
class MyModule {
public:
void print_msg();
};
void MyModule::print_msg() {
printf("hello from module\n");
}
// main.cpp
class MyModule {
public:
void print_msg();
};
int main() {
MyModule a;
a.print_msg();
}
We can compile and run it with
g++ main.cpp -c -o main.o
g++ mod.cpp -c -o mod.o
g++ main.o mod.o -o main
./main
The above works fine, but if I move the definition of MyModule::print_msg inside the class:
// mod.cpp
#include <cstdio>
class MyModule {
public:
void print_msg() { printf("hello from module\n"); }
};
I get an 'undefined reference' error for compiling main:
g++ main.cpp -c -o main.o # OK
g++ mod.cpp -c -o mod.o # OK
g++ main.o mod.o -o main # undefined reference error
/usr/bin/ld: main.o: in function `main':
main.cpp:(.text+0x23): undefined reference to `MyModule::print_msg()'
collect2: error: ld returned 1 exit status
I know that the former is the standard way and the class definition should go to a header file, but I wonder why the second method doesn't work.
Functions defined inside the class are implicitly inline. C++ requires:
The definition of an inline function [or variable (since C++17)] must be reachable in the translation unit where it is accessed.
Since you only defined it in mod.cpp, no definition is reachable in main.cpp, and compilation fails.
Typically, you'd put the definition of the class, and the definition of all functions defined within it, in a header file to be included by all users of the class. The functions defined outside the class then go in a .cpp file. That way a single consistent definition of all the inline functions is available to all users of the class, and you're not repeating the definition of the class in each .cpp file manually.
In the following code, I create a Builder template, and provide a default implementation to return nothing. I then specialize the template with int, to return a value 37.
When I compile with -O0, the code prints 37, which is the expected result. But when I compile using -O3, the code prints 0.
The platform is Ubuntu 20.04, with GCC 9.3.0
Can anyone helps me understand the behavior?
builder.h
class Builder {
public:
template<typename C>
static C build() {
return 0;
}
};
builder.cc
#include "builder.h"
template<>
int Builder::build<int>() {
return 37;
}
main.cc
#include "builder.h"
#include <iostream>
int main() {
std::cout << Builder::build<int>() << '\n';
}
makefile
CXX_FLAG = -O0 -g
all:
g++ $(CXX_FLAG) builder.cc -c -o builder.o
g++ $(CXX_FLAG) main.cc builder.o -o main
clean:
rm *.o
rm main
You should add a forward declaration for build<int>() to builder.h, like so:
template<>
int Builder::build<int>();
Otherwise, while compiling main.cc, the compiler sees only the generic template, and is allowed to inline an instance of the generic build() function. If you add the forward declaration, the compiler knows you provided a specialization elsewhere, and will not inline it.
With -O3, the compiler tries to inline, with -O0 it will not inline anything, hence the difference.
Your code actually violates the "One Definition Rule": it will create two definitions for Builder::build<int>(), but they are not the same. The standard says the result is undefined, but no diagnostics are required. That is a bit unfortunate in this case, as it would have been helpful if a warning or error message was produced.
I noticed that if I compile this
int x() { }
I get a warning, as usual:
$ clang++-7 -pthread -std=c++17 -o main main.cpp
main.cpp:5:11: warning: control reaches end of non-void
function [-Wreturn-type]
int x() { }
^
1 warning generated.
$ ./main
however, if the function takes any template arguments:
template <typename y>
int x() { }
no warnings appear.
$ clang++-7 -pthread -std=c++17 -o main main.cpp
$ ./main
Is this a bug in the compiler?
Until you call (or otherwise odr-use) that function template specialisation, instantiating it, the actual resulting function does not exist, and you won't get any diagnostics about it unless it is syntactically invalid.
We cannot see the rest of your program as you did not think a reproducible example was important, but presumably you did not do this in your program.
Write x<int>(); in your main function and you'll see the warning you wanted.
I found an example where the output is different depending on the optimization settings (-O3 vs none), yet GCC 4.8.2 produces no warnings, even with the -std=c++11 -Wall -pedantic options.
In this particular case, I'm assuming that "forgetting" the commented line in header.h is a mistake, and with -O3, c<int>::get() gets inlined.
However, is there any way to protect yourself against these kinds of mistakes -- a compiler or linker option perhaps?
header.h:
#ifndef HEADER_H
#define HEADER_H
template<class T>
struct c
{
int get() { return 0; }
};
// template<> int c<int>::get();
#endif
imp.cpp:
#include "header.h"
template<>
int c<int>::get()
{
return 1;
}
main.cpp:
#include <iostream>
#include "header.h"
int main()
{
c<int> i;
std::cout << i.get() << '\n'; // prints 0 with -O3, and 1 without
}
build:
c++ -std=c++11 -pedantic -Wall -O3 -c imp.cpp
c++ -std=c++11 -pedantic -Wall -O3 -c main.cpp
c++ -std=c++11 -pedantic -Wall -O3 imp.o main.o
What you get if you have the line in your header-file, is a declaration of an explicit specialization for that member-function.
Thus, main.cpp is assured of a definition in some other compilation-unit, and things work.
If you leave it out, you have a violation of the ODR:
That specific instantiation of your class-template is different in the compilation-units. Resulting in "ill-formed, no diagnostic required", so anything goes.
And there is (currently?) no compiler-option to force gcc to diagnose it.
The true mistake here is the way you're laying out your source files and building them. When c<int>::get() is used, its definition should be available in order to instantiate the template. To fix this, header.h should #include "imp.cpp" rather than the other way around. You may want to rename imp.cpp to imp.inl or something else.
When you define templates which are used outside of a single .cpp file, those definitions should be visible to anyone who includes their header.
As an aside: I don't think there's any way to make the compiler or linker warn you about what you've done here. But if you structure your project as I've described, this problem won't happen, because the forward declaration will be unnecessary.
This is wholy mysterious to me. I'm using g++ on ubuntu, and this is some of my code (with class names change, but nothing else because I'm still using stubs everywhere):
Bob.hpp
template <class A>
class Bob : public Jack<Chris, A>
{
public:
Bob(int x1, int x2, float x3 = 1.0, float x4 = 2.0, float x5 = 3.0) throw(Exception);
virtual ~Bob();
};
I implemented in another file like this:
Bob.cpp
template <class A>
Bob<A>::Bob(int x1, int x2, float x3, float x4, float x5) throw(Exception)
{
}
template <class A>
Bob<A>::~Bob()
{
}
and I used it like this:
main.cpp
int main()
{
Bob<Alice> instance(1, 2);
}
Compiling with:
g++ -c Bob.cpp -o Bob.o
g++ -c main.cpp -o main.o
g++ -L"libs" -llib main.o Bob.o prog
gives me
main.o: In function main':
main.cpp:(.text+0x1fd): undefined reference toBob::Bob(int, int, float, float, float)'
collect2: ld returned 1 exit status
I am completely stumped. Changing the order with the g++ linking stage makes no difference. Compiling the object files generates no problems. And Why an undefined reference when I implemented the constructor? If anyone could shed any light on this, it's be much appreciated.
You need to move the code from Bob.cpp into Bob.hpp. When the compiler sees the definitions of Bob::Bob and Bob::~Bob in Bob.cpp, it does not know what types of Bob are actually going to be instantiated (i.e. Bob<int> vs Bob<SomeClass> and the code for them isn't generated.
Alternatively, you can still place the code in the Bob.cpp file, but you need to declare which types of Bob are going to be instantiated, e.g.:
Inside of Bob.cpp:
template
class Bob<Alice>;
The declarations and definitions of the class template member functions should all be in the same header file.
When compiling Bob.cpp, the compiler has both the declarations and the definitions available. At this point the compiler does not need to generate any definitions for template classes, since there are no instantiations. When the compiler compiles main.cpp, there is an instantiation: template class Bob<Alice>. At this point the compiler has the declarations but no definitions!
In addition to the issues raised by others, libraries must come last on the GCC command line. Instead of:
g++ -L"libs" -llib main.o Bob.o prog
you want:
g++ -L"libs" main.o Bob.o prog -llib
Where do you think the constructor of Bob<Alice> should be defined? It wasn't defined in Bob.cpp, because there was no mention of a Bob<Alice> in Bob.cpp. There was a template, which could have been used to define Bob<Alice> when Bob.cpp was compiled into Bob.o, but it wasn't.
Put the template definition in Bob.hpp, or Bob<Alice> in Bob.cpp.