Jump to content
  • Advertisement
Sign in to follow this  
yacwroy

C++: Are typedef effects perfectly opaque?

This topic is 3722 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I build a library using: typedef int foo; And then link to this library from code built using typedef int bar; Am I guaranteed that (for any compliant compiler on any system) if there's a foo'd match in the library for a bar'd function call in the main program, that these will link? Even if the type is a template? Even if one isn't a typedef but just 'int' or whatever? Even if one is a member function of a template class whose template parameter is this typedef? Even if another library uses the same typedef name to mean something different?

Share this post


Link to post
Share on other sites
Advertisement
Yes, typedefs are only synonyms, not a new construction.

- The first two are kosher.
- The third is kosher too, I think (I'm not 100% sure on what you're asking)
- The fourth depends upon namespaces and collisions.


Please don't rely on this though. Who knows what other libraries will do.

Share this post


Link to post
Share on other sites
The fourth one could potentially get you into trouble. It violates the "one definition rule". Offhand, I can't think of any specific problems it would cause on compilers I'm familiar with--name mangling works on the actual type name, not the typedef name--but it's technically incorrect.

Share this post


Link to post
Share on other sites
Thanks for your replies.
Here's why I'm asking:

- The fourth point is my main concern.

By using a different include path I can alter what a typedef means if the typedef is declared inside a header whose absolute location will vary based on the supplied include path list.

This way I can compile the same library for different variables without resorting to predefined macros. If the types are template parameters, I can provide different specialisations of the same template using the same code, and use them at the same time.

ie:
g++ -o proj/libs/mylib.char.o proj/src/mylib.cpp -I proj/mytype/char
... and more cpp files
ar... (can't remember ar syntax)
(where proj/mytype/char/type.hpp does: typedef char mytype)

g++ -o proj/libs/mylib.wchar_t.o proj/src/mylib.cpp -I proj/mytype/wchar_t
... and more cpp files
ar... (can't remember ar syntax)
(where proj/mytype/wchar_t/type.hpp does: typedef wchar_t mytype)

Now if I want to parse (with a templated parser) a wide-char text file and a byte-char text file using the same parser code within the same binary (for example a single-binary text-editor with RE's), I can, by including the two libraries above.
And I don't need to recompile the parser twice (for the two template versions) when I build the main app since it's defined externally in the two libraries, and not inlined.

It was a toss-up between providing macros or varying typedefs via include directories to the compiler, but I think include directories is more consistent for me.

Checking up on the One Definition Rule (via wikipedia), I don't think this is a technical violation, as each object file is built with only one version of the typedef, and I'm stating what I'm doing explicitly in comments.

Share this post


Link to post
Share on other sites
Quote:
Original post by yacwroy
Checking up on the One Definition Rule (via wikipedia), I don't think this is a technical violation, as each object file is built with only one version of the typedef, and I'm stating what I'm doing explicitly in comments.

The definition needs to be consistent across all compilation units, not merely one object file. It's actually not even possible to violate the ODR with only one object file. I think you may have misunderstood the ramifications of the rule.

Share this post


Link to post
Share on other sites
Quote:
Original post by yacwroy
Thanks for your replies.
Here's why I'm asking:

- The fourth point is my main concern.

By using a different include path I can alter what a typedef means if the typedef is declared inside a header whose absolute location will vary based on the supplied include path list.

This way I can compile the same library for different variables without resorting to predefined macros. If the types are template parameters, I can provide different specialisations of the same template using the same code, and use them at the same time.


Why not just use a template parameter with the typedef name, and instantiate the template with the various types?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!