You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Then the two instances will share the same vtable, meaning calling tn2() on either will return the same value, whereas calling tn1() will return different values.
The assertion in test() will fail for one of the two instances.
To test this, save the 3 files above and add main.cc as:
// main.cc
void foo ();
void bar ();
int main (int argc, char const *argv[])
{
foo();
bar();
return 0;
}
Then in Terminal run:
% clang++ {foo,bar,main}.cc && ./a.out
The result I get is:
Assertion failed: (tn1() == tn2()), function test, file ./header.h, line 10.
I also attached an archive with the four files + build.sh.
The text was updated successfully, but these errors were encountered:
Extended Description
If in a header I declare a template class like this:
// header.h
#include <assert.h>
#include
template
class klass
{
char const* tn1 () const { return typeid(T()).name(); }
virtual char const* tn2 () const { return typeid(T()).name(); }
public:
void test () const { assert(tn1() == tn2()); }
};
Then in two different translation units instantiate the template class with identically named types, but from anonymous namespaces, example:
// foo.cc
#include "header.h"
namespace { struct my_type_t { }; }
void foo () { klass<my_type_t> foo_instance; foo_instance.test(); }
// bar.cc
#include "header.h"
namespace { struct my_type_t { }; }
void bar () { klass<my_type_t> bar_instance; bar_instance.test(); }
Then the two instances will share the same vtable, meaning calling tn2() on either will return the same value, whereas calling tn1() will return different values.
The assertion in test() will fail for one of the two instances.
To test this, save the 3 files above and add main.cc as:
// main.cc
void foo ();
void bar ();
int main (int argc, char const *argv[])
{
foo();
bar();
return 0;
}
Then in Terminal run:
% clang++ {foo,bar,main}.cc && ./a.out
The result I get is:
Assertion failed: (tn1() == tn2()), function test, file ./header.h, line 10.
I also attached an archive with the four files + build.sh.
The text was updated successfully, but these errors were encountered: