1

I am looking at two source codes. One uses this:

using boost::property_tree::ptree;

And the other uses this:

namespace ptree = boost::property_tree::ptree;

What is the difference?

  • One imports the namespace into scope in which `using` is written, and other doesn't? – Algirdas Preidžius Apr 20 '17 at 10:27
  • 2
    I actually have no idea why you are downvoted. It's a half decent, clear question – StoryTeller - Unslander Monica Apr 20 '17 at 10:30
  • @StoryTeller I downvoted due to the lack of research. – Algirdas Preidžius Apr 20 '17 at 10:31
  • 2
    @AlgirdasPreidžius - Did you come up with a possible duplicate when determining "lack of research"? – StoryTeller - Unslander Monica Apr 20 '17 at 10:32
  • @StoryTeller And, why do you think _lack of research_ should mean research on SO? These sorts of questions are answered, pretty, well, by reading documentation, or a [C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). I could come up with bunch of _what is the difference between_ kind of questions, that aren't covered on SO, but that wouldn't make them good questions, due to the reason explained above. – Algirdas Preidžius Apr 20 '17 at 10:39
  • 2
    @AlgirdasPreidžius - I think that, because SO is designed as a Q&A site for programming questions. If a question wasn't asked before, and is stated clearly, it deserves to be asked. Downvoting isn't to punish people for not knowing, it's for marking low quality posts. – StoryTeller - Unslander Monica Apr 20 '17 at 10:40
  • 3
    @AlgirdasPreidžius - [Here's another question "answerable by a C++ book"](http://stackoverflow.com/questions/7067793/what-is-this-crazy-c11-syntax-struct-bar-foo), and it's highly upvoted for actually being good and insightful. This question here has the potential of helping future readers looking for the info, thus it's good. – StoryTeller - Unslander Monica Apr 20 '17 at 10:41
  • @ Algirdas Preidžius By the time I finish reading those books I would already be in heaven and eating heaven burgers. I come to SO to get quick and easy answers, as did you :o) –  Apr 20 '17 at 10:49
  • 1
    @Theburgerman _I come to SO to get quick and easy answers, **as did you**_ No I didn't. In addition, I have read several C++ books, and I am still not in heaven. – Algirdas Preidžius Apr 20 '17 at 10:51
  • @Theburgerman the answer you selected is wrong. Just sayin'. – juanchopanza Apr 20 '17 at 21:58

2 Answers2

7

With

using boost::property_tree::ptree;

you pull in the boost::property_tree::ptree class into the current namespace. From that point onward you can use ptree instead of boost::property_tree::ptree.

With

namespace ptree = boost::property_tree::ptree;

you should have an error since boost::property_tree::ptree is a class and not a namespace. Otherwise (if it were a namespace) it would create an alias for the namespace (like how pt is used in the Boost property tree tutorials).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
-1

The using statement brings a namespace into scope. The namespace statement defines an alias (to use it you would have to access it with the :: operator).

EDIT: See programmer dude's answer for the correct answer.

Aidenhjj
  • 1,249
  • 1
  • 14
  • 27
  • 2
    In this example, the `using` statement *does not* bring a namespace into scope, because `ptree` isn't a namespace. – juanchopanza Apr 20 '17 at 11:17