2
int main(){
 int a = 5; // copy initialization.

 int b(a); // direct initialization.
 int *c = new int(a); // also direct initialization, on heap.

 int d = int(a); // functional cast, also direct initialization? 
                 // or copy initialization?

 return 0;
}

I have 2 questions on this:

1 - It's not clear to me if int(a); is only a cast, or if it's also an initializer. Is d being copy initialized or is it being direct initialized? i.e is the expression equal to this:

int d = 5;

or this

int d(5);

2- I want to confirm if new int(a); is only an initializer and not a cast to int. Also want to confirm if it's direct initialization and not copy initialization.

I have read section (3) of the reference but the answer is not clear to me: https://en.cppreference.com/w/cpp/language/direct_initialization

  1. initialization of a prvalue temporary (until C++17)the result object of a prvalue (since C++17) by functional cast or with a parenthesized expression list
10
  • 1
    Not sure... is this a language-lawyer tag question? Commented Jul 15, 2021 at 20:24
  • Side note: Good presentation on initialization and how deep the rabbit hole goes Commented Jul 15, 2021 at 20:28
  • 1
    int d = int(a); is the same as int d = (int)a; Commented Jul 15, 2021 at 20:56
  • Relevant section of the standard: eel.is/c++draft/expr.type.conv Commented Jul 15, 2021 at 21:13
  • I also found this stackoverflow.com/a/1051468. So like @Eljay said, int(a) creates a temp variable and d gets copy initialized with it? In case a class is used ie test d = test(a); the constructor of test gets called? how about the new case, it's always a direct initialization and not a cast, or is it a copy initialization? Commented Jul 15, 2021 at 21:16

1 Answer 1

4

Your analysis is missing some steps.

int *c = new int(a);
  1. This performs direct initialization of a dynamically-allocated int instance.

  2. The new operator evaluates to a pointer prvalue (type = int*) which is the address of this new int object.

  3. c is copy-initialized from the pointer prvalue.

int d = int(a);
  1. This performs direct initialization of a temporary int instance, which is a prvalue (type = int).

  2. d is copy-initialized from this prvalue.

In both cases, the copy constructor call is eligible for elision in all C++ versions, and elision is guaranteed in the latest Standard.

Sign up to request clarification or add additional context in comments.

26 Comments

The new operator evaluates to a pointer prvalue (type = int* &&) Where did && come from?!
@Dan: "this points to the class instance". Exactly. The input to the conversion operation is the instance of test and the output is int. I'm contrasting with your snippet struct test { int operator()(int in) { /**/ } }; which I meant was supposed to transform in into the result int, but is not a conversion.
@BenVoigt Look just one paragraph above :-) : "[...] names the destructor of T if T is a class type, otherwise the id-expression is said to name a pseudo-destructor". It specifically avoids talking about destructors for non-class types.
Most of the type traits query the behavior of types; their descriptions hardly ever mention special member functions, after a qualifier like "if T is a class type". The one exception I can find, is_nothrow_destructible, is most likely an oversight, because is_trivially_destructible carefully separates class and non-class types. The traits also work on references and functions too, and that doesn't mean we can talk about special functions of those kinds of types. I don't see any explicit allowance for talking about special member functions of primitive types in any of these examples.
"Denotes a destructor of type T" describes the syntactic form of an id-expression, see eel.is/c++draft/expr.prim.id#unqual-1.sentence-4
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.