0

I have noticed that both int num(5) and int num = 5 both compile and assign 5 to num.

Questions

  1. Could you help me understand why int num(5) work? This syntax looks like initializing an object
  2. What is the difference between the two in terms of speed and stuff that goes behind

Pardon me if it's a silly question. I am a noob at C++ and in process to learn stuff.

  • 1
    For speed there should be no difference if you are using any common compiler that optimises the code. – Lily Nov 06 '20 at 15:41
  • Personally I do not know the exact difference the syntax implies, I remember seeing a rule that using `int num{5}` is the best way to create a variable but I do not remember why. – Lily Nov 06 '20 at 15:43
  • 1
    This video [The Nightmare of Initialization in C++](https://www.youtube.com/watch?v=7DTlWPgX6zs) by Nicolai Josuttis goes through all the different ways you can do initialization in C++. – Eljay Nov 06 '20 at 15:44
  • Thank you Eljay. I'll go through it :) – Ashutosh Bhushan Nov 06 '20 at 15:46
  • From testing just now, both create the exact same assembly, as I expected (although this is my first time generating assembly so I might have made a mistake). – Lily Nov 06 '20 at 15:52
  • 1
    @ColonD I would recommend https://godbolt.org/ for inspecting compiler output. – François Andrieux Nov 06 '20 at 15:53
  • @FrançoisAndrieux thank you for the recommendation, it seems to produce a nicer to look at assembly than Visual Studio does – Lily Nov 06 '20 at 15:55
  • Oh wow it evens lets you hover over lines to see it's assembly. Regardless of syntax, it gets compiled down to `mov dword ptr [rbp - 4], 5` – Lily Nov 06 '20 at 16:03

1 Answers1

3

Could you help me understand why int num(5) work? This syntax looks like initializing an object

For the basic case (where the programmer explicitly types in a line of code like int num(5); or int num = 5;, having two different syntaxes that both do the same thing seems unnecessary, and it is.

However, once you get into templates, you'll find cases where you want to write a template that can work with both user-defined classes and with the built-in types. For example, here is a template I wrote the other day to save (and later restore) the state of an arbitrary value:

template<class T> class SaveGuard
{
public:
   SaveGuard(T & saveMe) : _saveMe(saveMe), _tempHolder(saveMe) {/* empty */}  // save the current value
   ~SaveGuard() {_saveMe = _tempHolder;}  // restore the saved value

private:
   T & _saveMe;
   T _tempHolder;
};

Note that in this case, the _tempHolder(saveMe) clause is syntactically the same as the int(5) syntax, and since the int(5) syntax is supported, I can declare a SaveGuard<int> guard(blah); in my program and it will work as expected. Alternatively I can declare a SaveGuard<MyClassType> guard(blah); and the template will work for that as well, using the copy-constructor of the MyClassType class.

What is the difference between the two in terms of speed and stuff that goes behind

There's no difference in speed in your example; the compiler will generate the same assembly code for either syntax.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • 3
    Useful references : `int num(5);` is [Direct initialization](https://en.cppreference.com/w/cpp/language/direct_initialization) and `int num = 5;` is [Copy initialization](https://en.cppreference.com/w/cpp/language/copy_initialization). They are indeed identical for `int`. – François Andrieux Nov 06 '20 at 15:53
  • Thanks a lot for this clarification. Shall I delete the question because it's duplicate or shall I let it be ? – Ashutosh Bhushan Nov 06 '20 at 15:57
  • 1
    @AshutoshBhushan It is helpful to leave duplicates. It acts as "sign posts" for other users who would otherwise maybe not find the question that this question is a duplicate of. – François Andrieux Nov 06 '20 at 16:09
  • Okay, understood :D – Ashutosh Bhushan Nov 06 '20 at 16:16