41

I think that it has a lot of information about it but I don't know how this is called. I cannot understand difference between next two strings of code:

Object obj();

And

Object obj = Object();

Can you please explain? Or at least say how to call it.

3
  • Okay, why then I get an issue when I use Object obj() but don't get when use Object obj = Object()? Dec 21, 2015 at 20:09
  • @ghostman well you should be using Object obj;, without the parentheses
    – dabadaba
    Dec 21, 2015 at 20:10
  • 3
    @juanchopanza now that I noticed the parentheses you're right, they're different. I assumed OP said Object obj, not Object obj().
    – dabadaba
    Dec 21, 2015 at 20:13

4 Answers 4

51
Object obj();

is not an instantiation of object, it is a declaration of a function obj which takes no arguments and returns an instance of Object.

Object obj;

is a default initialization, i.e. instantiation with implicit constructor (thus, default implicit constructor or user-defined non-explicit constructor with no parameters), and this declaration calls implicit constructors of non-POD Object members, and for POD-types it does not initialize them (they will not be zeroed). This is right for members of members of Object and so on recursively.

Object obj{};

is a list initialization or aggregate initialization (if Object is an aggregate). Those it called differently, for empty braces, behavior is the same: all members of POD-types are zero-initialized, and non-POD are default-initialized.

Object obj = Object();

theoretically is a two-step statement: 1) create temporary Object instance; 2) then construct obj by copy constructor/move constructor/copy operator/move operator. But in practice it will be default-constructed with copy/move-elision in mind (it is enabled on all modern compilers by default even with all optimizations off, you must disable elision explicitly). Better do not use this variant.

Pre-Conclusion

Choose

Object obj;

or

Object obj{};

Choose first if you want fast initialization with no zeroifying its POD-members. Choose second if you want to be sure that all its POD-members will be zero after instantiation of Object.

Practically, before first reading from its members, the both variants have the same speed in runtime on all modern OSes.

So...

Conclusion

Use value-initialization:

Object obj{};

unless you need a realtime performance on exotic systems.

6
  • 1
    Should note that Object obj{}; is a C++11 feature, and was not available in earlier versions. I've had to deal with this regularly, as some of our source code is shared by another team which for technical reasons is still using C++09, so any files common to both projects cannot use this notation. Dec 22, 2015 at 15:54
  • @DarrelHoffman Come on, it's end of 2015 today! C++14 IS a standard, C++17 coming.
    – vladon
    Dec 22, 2015 at 15:56
  • 1
    Doesn't change the fact that a lot of companies are still using the older standards. The OP used the C++ tag and not the C++11 (or later) tags, so you can't assume they're using the latest standard. Dec 22, 2015 at 15:58
  • @DarrelHoffman Why not you remember Borland Turbo C++ for DOS then? Lot of companies flooded with elderly so called programmers which simply don't want to learn anything new. I have met a lot of those. It is very bad. Also I have met several modern companies which use C++ on the edge of new standards in production (even for embedded systems). And they are the best.
    – vladon
    Dec 22, 2015 at 16:07
  • 2
    @vladon: There's a lot of overhead in terms of switching costs, and many companies are still forced for a variety of reasons to work with legacy code. Sure, if it were up to me I'd use the latest and greatest of everything, but we don't always have the option to do so, especially at larger companies that have existed for more than a decade. I've gotten in trouble more than once for using C++11 style code which works in my project but won't compile for other teams that are using the same files with older compilers. Just wanted to point out that not everybody has that choice. Dec 22, 2015 at 16:19
31
Object obj();

declares a function, not an object! This is an instance of the "Most Vexing Parse".

Object obj = Object();

requires Object to have an accessible move constructor or copy constructor (although the compiler might end up eliding the move/copy).

Simple ways to just create an object include:

Object obj;
Object obj{};
2
  • Thank you! I understood, don't know about it. Dec 21, 2015 at 20:11
  • 5
    Object obj; and Object obj{}; are different.
    – vladon
    Dec 21, 2015 at 20:40
21
Object obj();

Declares a function names obj that returns a Object and takes no parameters. This is generally not what you want.

Object obj = Object();

Declares an Object named obj then is copy initialized with a temporary default constructed Object. You normally don't want to do this either unless you are resetting an object back to a default state.

Generally if you want to construct without calling a constructor you can use

Object obj;
//or
Object obj{};
2
  • Thank you for the advanced answer! Dec 21, 2015 at 20:13
  • 5
    Object obj{}; is value initiation, not default initialization. That can make a difference in some cases. Dec 21, 2015 at 20:32
8

Big difference.

The first one is a function prototype to a function taking no arguments and returning an Object.

The second one instantiates an Object by calling the default constructor. For the avoidance of doubt the assignment operator is not called.

Drop the parentheses in the first case to make the statements equivalent.

2
  • Dropping the parenthesis does not make the statements equivalent! Object obj = Object(); creates a temporary instance of Object, then copy-constructs obj from temporary. In most cases there will be copy-elision, but there are rare cases where this makes sense.
    – vladon
    Dec 21, 2015 at 20:26
  • 3
    The second one copy/move constructs obj from a temporary. This is different because it will not compile if the object is not MoveConstructible . Also, if it is movable, it may have different behaviour from Object obj; although it would be considered bad style to actually write code that does have different behaviour.
    – M.M
    Dec 21, 2015 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.