Skip to content

Wrong vtable for template class instantiated with anonymous type #10492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
llvmbot opened this issue Jun 10, 2011 · 2 comments
Closed

Wrong vtable for template class instantiated with anonymous type #10492

llvmbot opened this issue Jun 10, 2011 · 2 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla c++

Comments

@llvmbot
Copy link
Member

llvmbot commented Jun 10, 2011

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.

@llvmbot
Copy link
Member Author

llvmbot commented Jun 10, 2011

assigned to @efriedma-quic

@efriedma-quic
Copy link
Collaborator

r132861.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 3, 2021
cyndyishida pushed a commit to cyndyishida/llvm-project that referenced this issue Apr 17, 2025
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++
Projects
None yet
Development

No branches or pull requests

2 participants