Skip to content

Instantly share code, notes, and snippets.

@ywkaras
Created February 7, 2024 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ywkaras/ebc6bfbb943acb73c4dfcc174da2e49b to your computer and use it in GitHub Desktop.
Save ywkaras/ebc6bfbb943acb73c4dfcc174da2e49b to your computer and use it in GitHub Desktop.
Template link hell
$ cat a.cc
template <typename T>
struct C
{
static void f1();
static void f2();
};
void g()
{
C<int>::f1();
C<double>::f2();
}
$ cat b.cc
template <typename T>
struct C
{
static void f1();
static void f2();
};
template <> void C<int>::f1() {}
template<typename T> void C<T>::f2() {}
void g();
int main()
{
g();
C<double>::f2();
return 0;
};
$ gcc -Wall -Wextra -pedantic -c a.cc
$ gcc -Wall -Wextra -pedantic -c b.cc
$ nm -C a.o
0000000000000000 T g()
U C<double>::f2()
U C<int>::f1()
$ nm -C b.o
0000000000000007 T main
U g()
0000000000000000 W C<double>::f2()
0000000000000000 T C<int>::f1()
$ gcc -Wall -Wextra -pedantic a.o b.o
$ ./a.out
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment