user7533668
user7533668

Reputation:

c++ What is the difference between namespace and using

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?

Upvotes: 1

Views: 175

Answers (2)

Aidenhjj
Aidenhjj

Reputation: 1289

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.

Upvotes: -1

Some programmer dude
Some programmer dude

Reputation: 409482

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).

Upvotes: 7

Related Questions