Skip to content

Wrong vtable for template class instantiated with anonymous type #10492

Closed
@llvmbot

Description

@llvmbot
Bugzilla Link 10120
Resolution FIXED
Resolved on Jun 10, 2011 16:58
Version 2.9
OS MacOS X
Attachments Sample project to show problem
Reporter LLVM Bugzilla Contributor
CC @DougGregor,@efriedma-quic

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.

Metadata

Metadata

Assignees

Labels

bugzillaIssues migrated from bugzillac++

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions