-2

What is the difference between:

type name (value)  //notation and
type name = value  // ?

I am currently studying from Bjarne Stroustrup's "Programming: Principles and Practice using C++", and in the fourth chapter he introduces conversion notations.

I have found however that writing double x (5); and double x = 5; outputs the same result in any occasion. What is the difference between these two? In practice, on the first example, I'm making a conversion, while on the second one I'm making an initialization, but the result is the same.

I have experimented with different values for the variables I'm initializing / converting, but the result was the same.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
bulug
  • 39
  • 3
  • Please don't roast i know it sounds very dumb and it's not explained clearly – bulug May 30 '23 at 21:29
  • There is no difference until you get to class types (and the `explicit` keyword). You can use whichever you prefer until then (and really in most practical situations as well). Both of them are _initialization_ (the first _direct-initialization_ and the second _copy-initialization_) and both do an _implicit conversion from `int` to `double`_ as part of the initialization process in your example. – user17732522 May 30 '23 at 21:52
  • If both compile, there's no difference. Some classes are written in a way that bans the second form (`explicit` on the constructor). – HolyBlackCat May 30 '23 at 22:00
  • Reopened. The claimed duplicate was much narrower, only about copy construction. – Pete Becker May 31 '23 at 03:03
  • @PeteBecker Added more accurate duplicates. – Jason May 31 '23 at 05:01
  • @PeteBecker I have literally no idea what you're talking about. As far as I can tell, the original duplicate target asks *exactly* the same question as this one. Is there any possible answer one could post to this question that would not apply to the original target? – cigien May 31 '23 at 21:12
  • @cigien — if you add `type value;` before the code in this question you get the original duplicate. That constraint is not present in this question. – Pete Becker Jun 01 '23 at 02:44

1 Answers1

1

Depending on the type, it can have different meanings.

Assembly point of view:

  1. For int, it is always identical from an assembly point of view for some specific compiler: https://godbolt.org/z/7KTMccr1o
  2. For complex types, it can get quite interesting with constructors, copy constructors and move constructors. https://godbolt.org/z/oKdGGTjoo

Counter example of code in C++ where you get different behavior for:

type name(value)

type name = value

#include <iostream>
using namespace std;

class A
{
    int a;
public:
    A(int tmp){ a = tmp;};
    ~A(){};
    A(A && tmp){ a = tmp.a+1;};
    friend ostream& operator<<(ostream& os, const A& a);
};

ostream& operator<<(ostream& os, const A& a)
{
    os << a.a;
    return os;
}

int main()
{
    A a(0);
    A b = 0;
    std::cout << a << " " << b << std::endl;

    return 0;
}

You also have to use a compiler flag to get different behavior: -fno-elide-constructors.

Radovm
  • 26
  • 3
  • Comparing c++ code to assembly doesn't explain things to a beginner who is asking a C++ question not an assembly question. – Jason May 31 '23 at 05:01
  • Yes you are right, I added a counter example in C++ with custom type. The "smallest" reproducible counter example I could think off in a rush. Hopefully that will help answering. – Radovm May 31 '23 at 05:40
  • Thanks a lot!! I don't really understand everything you wrote but i'll get there, eventually – bulug May 31 '23 at 13:46
  • Great to hear that, I'm certain you will, slowly and steadily the turtle wins the race! – Radovm May 31 '23 at 20:43