Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

c++ - typeid result across different dll's

I have two dlls which both declare a templated type, let's call A. If the declaration of A is sufficiently intricate, it happens that the result of typeid(A).name() is different when called in functions in two different dll's.

example:

DLL1:

struct MyType: public A< TEMPLATE_LIST_OF_A >{}

void f(){
std::string name1 = typeid(A).name();
}

DLL2:

struct MyType: public A< TEMPLATE_LIST_OF_A >{}
    void f(){
    std::string name2 = typeid(A).name();
    }

for example name1 could be something like: "???MyType??? etc" while name2 could be "???A??TEMPLATE_LIST_OF_A etc".

Which actually makes quite sense to me, but is there is a way, provided that the names used are the samem to guarantee that name1==name2 ?

thanks, rob

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not only is there no way to guarantee that typeid().name() is the same in different DLLs, the standard makes almost no guarantees about the string returned at all. Specifically, it is not guaranteed to be a) meaningful, b) unique for different types, c) the same for identical types.

As a quality of implementation issue, you can probably assume that these three conditions hold, but especially for complicated template types I wouldn't be surprised if you could find cases where they were violated in a specific compiler.

The relevant parts of the 98 standard are 5.2.8 and 18.5.1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...