Armen Tsirunyan
Armen Tsirunyan

Reputation: 133014

shared pointer forward declaration

I have an interface Interface. I also have a .h file InterfaceFwd.h which looks something like

#ifndef Blah
#define Blah
#include <boost/shared_ptr.hpp>
class Interface;
typedef boost::shared_ptr<Interface> InterfacePtr;
#endif

I also have Interface.h

#ifndef SomeOtherBlah
#define SomeOtherBlah
class Interface
{
   virtual ~Interface()
   { 
   }
   ... 
};
typedef boost::shared_ptr<Interface> InterfacePtr;
#endif

Do I need to worry that if both files are included there will be duplicate declaration of InterfacePtr? On my compiler this compiles fine, but does the standard One-Definition Rule allow multiple identical typedef-declarations? Also, do you think I should include InterfaceFwd.h into Interface.h instead of redeclaring InterfacePtr or it's fine as it is?

Thanks in advance

Upvotes: 0

Views: 2204

Answers (1)

CB Bailey
CB Bailey

Reputation: 791929

The one definition rule doesn't apply to typedefs. A typedef (on its own) doesn't define a new variable, function, class type, enumeration type or template. You are explicitly allowed to redefine a previous typedef-name to refer to the type that it already refers to.

7.1.3 [dcl.typedef]:

In a given non-class scope, a typedef specifier can be used to redefine the name of any type declared in that scope to refer to the type to which it already refers.

Upvotes: 2

Related Questions