5

Is it legal and good programming style to use std::tr1::shared_ptr as std::shared_ptr placing using directive in corresponding header? Like this:

namespace std
{
   using tr1::shared_ptr;
}

I know that it's bad to pollute entire namespace but what about this case? Are there any hidden gotchas? Target compiler is VS2008 but compatibility with later versions is also desired.

Rost
  • 8,779
  • 28
  • 50
cassini
  • 85
  • 5
  • 3
    [Later versions should also support `std::tr1`](http://stackoverflow.com/a/2002972/241631). Compilers are not going to drop the entire namespace; they'll copy stuff from that to `std` just like you're attempting to do. So referring to the type as `std::tr1::shared_ptr` shouldn't be problematic for a long time. – Praetorian May 03 '13 at 20:07
  • @cassini What Praetorian said^ If you wanted to go your route, you'd have to do compiler checks in the preprocessor to make sure you don't get conflicts on C++11 supporting, and future, compilers, which is messy and not that flexible and not as portable. – leetNightshade May 03 '13 at 20:12
  • 1
    @leetNightshade Actually it’s all of the above. Granted, there’s unfortunately no “compiler supports feature X” check but for most purposes a simple preprocessor check for the C++ version should suffice, going forward. – Konrad Rudolph May 03 '13 at 20:15
  • 2
    Almost duplicate of http://stackoverflow.com/questions/10736046/importing-stdtr1-into-std-is-it-legal-does-it-improve-portability – Rost May 03 '13 at 22:06

1 Answers1

3

Technically, the Standard says that you enter the realm of Undefined Behavior if you do this:

17.6.4.2.1 Namespace std [namespace.std]

1 The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified.

But in practice, you are likely to get away with it. Heck, even Scott Meyers proposed a similarly undefined namespace alias trick in Effective C++ 3rd Ed. (Item 54, p.268) to use Boost functionality as a stopgap for missing tr1 functionality.

namespace std { using namespace tr1 = ::boost; }

Your using declaration is also undefined behavior, but go ahead and jump right in.

NOTE: comment it with a big fat warning, #define and #pragma around your compiler version and warnings, and as soon as you upgrade to a compiler/library that actually has std::shared_ptr, make sure to revisit that header and remove the code.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • To add to the final note: Comments are nice and all. However, they tend to be just that, comments. If you want something with a bit more persuasive powers use preprocessor directives instead. Something along the lines of #if defined( _MSC_VER ) && ( _MSC_VER > 1500 ) #error Please revisit the following code. #endif will probably trigger some sort of action when appropriate. – IInspectable May 03 '13 at 21:55
  • Thank you, but I would like to not revisit this code after upgrade to new compiler. – cassini May 03 '13 at 22:15
  • @cassini well, you are in Undefined Behavior Land, so you might want to eliminate that risk as soon as possible. Surely, a simple recompile wouldn't hurt too much? – TemplateRex May 03 '13 at 22:20