Discussion: Correct C++

Pages: 12
Sep 1, 2012 at 7:56pm
As the title gives away this is a discussion topic, not a question.

Wich is more correct C++:

int x(13);

or

int x = 13;
Sep 1, 2012 at 7:58pm
The first one is initialization, the second is assignment. There isn't much difference when dealing with an int.
Sep 1, 2012 at 8:01pm
AFAIK initialization is defined as a variable being given its first value, so aren't both initialization?

int x = 13; is more natural, to me anyway.
Sep 1, 2012 at 8:02pm
The both are equally correct in C++. The difference is that the first one is not allowed in C.
Sep 1, 2012 at 8:07pm
I thought they both invoked the constructor. Do they not?
Sep 1, 2012 at 8:13pm

@xhtmlx
I thought they both invoked the constructor. Do they not?


Fundamental types have no constructor.
Last edited on Sep 1, 2012 at 8:13pm
Sep 1, 2012 at 8:18pm
Fundamental types have no constructor.

They actually do, or at least, you can consider them to.

Built-in types also have default constructors. ($6.2.8)
Sep 1, 2012 at 8:23pm

@Dash
Built-in types also have default constructors. ($6.2.8)


What is $6.2.8?
Sep 1, 2012 at 8:25pm
Section 6.2.8, The C++ Programming Language by Bjarne Stroustrup.
Sep 1, 2012 at 8:30pm
It is incorrect statement. As I said fundamentals types have no constructor. You should read the C++ Standard instead of various books of some authors including Stroustrup. It is the C++ Standard that determines the C++ language. I did not find any reference in the C++ Standard that fundamental types have a constructor.
Last edited on Sep 1, 2012 at 8:33pm
Sep 1, 2012 at 8:30pm
Is there nothing said in the ISO standard?
http://www-d0.fnal.gov/~dladams/cxx_standard.pdf

I do not know how to find it, but it must be there?
Sep 1, 2012 at 8:34pm

@Krusing

I do not know how to find it, but it must be there?


No there is no such statement in the C++ standard.
Sep 1, 2012 at 8:37pm
I probably read it when I was reading about strings and thought it applied to all of the datatypes. Don't ask me why, that's just what I thought. Sorry for the mistake.
Last edited on Sep 1, 2012 at 8:37pm
Sep 1, 2012 at 8:45pm
1
2
3
4
5
6
void f(double d)
{
    int j = int();             // default int value
    complex z = complex();     // default complex value
    // ...
}


Section 6.2.8 Constructors

The value of an explicit use of the constructor for a built-in type is 0 converted to that type. Thus, int() is another way of writing 0. For a user-defined type T, T() is defined by the default constructor, if any.


Although it doesn't seem like int has an actual constructor.

Unfortunately, for a built-in type T, T(e) is equivalent to (T)e
Last edited on Sep 1, 2012 at 8:47pm
Sep 1, 2012 at 8:49pm
@Zephilinox

Section 6.2.8 Constructors

The value of an explicit use of the constructor for a built-in type is 0 converted to that type. Thus, int() is another way of writing 0. For a user-defined type T, T() is defined by the default constructor, if any.


Please, do not cite wrong statements. Firstly the C++ Standard has no such section as 6.2.8. Secondly you shall refer to the C++ Standard not some books about C++.
Last edited on Sep 1, 2012 at 8:49pm
Sep 1, 2012 at 8:56pm
I never said I was referring to the standard, but to be fair I didn't say I was referring to the book mentioned above either, the standard is a mess of technicalities that I can't decypher, and I'll quote whatever the hell I want, if you don't want to accept my quote that's fine with me, I'm just throwing it into the discussion.

I also said it doesn't seem like int has an actual constructor.
Last edited on Sep 1, 2012 at 8:56pm
Sep 1, 2012 at 9:31pm
The value of an explicit use of the constructor for a built-in type is 0 converted to that type. Thus, int() is another way of writing 0. For a user-defined type T, T() is defined by the default constructor, if any.

Yes.

> Although it doesn't seem like int has an actual constructor

No, it does not. Even so, a value initialized int is deemed to be constructed.

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized. - IS


An object that is value-initialized is deemed to be constructed and thus subject to provisions of this International Standard applying to 'constructed' objects, objects 'for which the constructor has completed,' etc., even if no constructor is invoked for the object’s initialization. - IS


Sep 1, 2012 at 9:31pm
closed account (zb0S216C)
Both are valid forms initialisation. The first form is called functional notation. The second is called plain initialisation. In my opinion, functional notation is the C++ way of initialisation. Since functional notation was not part of the C syntax, it leads me to believe it was added into C++ to mimic the syntax of a constructor call.

xhtmlx wrote:
"I thought they both invoked the constructor. Do they not?"

Integrated types do not have a constructor. Constructors are special member functions, which only classes can have. Because the basic integrated types are not classes, they cannot have constructors, nor can they have destructors.

Wazzak
Last edited on Sep 1, 2012 at 9:41pm
Sep 1, 2012 at 10:58pm
@Framework
Because the basic integrated types are not classes, they cannot have constructors, nor can they have destructors.


Though it can be written as

1
2
3
4
5
typedef int I;

I x;

x.~I();
Sep 1, 2012 at 11:09pm
@vlad from moscow

Is that not function declarations? Even tho it can be used with typedef, I would not use typedef for other than function prototype declaration.

I read somewhere that the use of typedefs can make code easier to maintain, you only need to change one statement in your source code instead of everywhere it appears...

Edit: But that's another story... =)
Last edited on Sep 1, 2012 at 11:10pm
Pages: 12