Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do invocations of std constructors need to be qualified?

Do invocations of std constructors need to be qualified with std::?

class whatever : public std::runtime_error
{
public:
    explicit whatever(const std::string& what) : runtime_error(what) {}
};                                            // ^ do I need std:: here?

It works on my compiler without the qualification, but I'm not sure whether that behavior is standard.

like image 413
fredoverflow Avatar asked Jul 06 '12 14:07

fredoverflow


2 Answers

No you don't. The names in the initializer list are looked up in the scope of the whatever class. This class scope includes names declared in base classes and the name of the base class (runtime_error) is inserted into the scope of std::runtime_error (this is standard behaviour for all classes).

Note that this doesn't work if the name that you use is a typedef for the actual class name. You can easily be tempted with, e.g., std::istream and friends. See here.

like image 166
CB Bailey Avatar answered Oct 06 '22 01:10

CB Bailey


There is no need for the qualification in the initializer list (to be honest I don't know if the qualification is even allowed there), as it is a base and will be found by lookup through the class.

like image 44
David Rodríguez - dribeas Avatar answered Oct 06 '22 01:10

David Rodríguez - dribeas