Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incomplete type not allowed when using new[] keyword

Tags:

c++

This code gives errors

incomplete type is not allowed

too many initializer values

string *ReturnTwoStringsInArray()
{
   return new string[]{"return1", "return2"};
}

This one works:

string *ReturnTwoStringsInArray()
{
   return new string[2]{"return1", "return2"};
}

And so does this one:

string arr[]{"return1","return"};

Shouldn't we be able to call new with no parameters in [] if the compiler can determine the needed size from the initializer list?

like image 462
luka_bur3k Avatar asked Apr 17 '20 23:04

luka_bur3k


People also ask

What is incomplete type not allowed in C++?

An incomplete class declaration is a class declaration that does not define any class members. You cannot declare any objects of the class type or refer to the members of a class until the declaration is complete.

What does incomplete type mean in GDB?

but no definition for struct foo itself, gdb will say: (gdb) ptype foo $1 = <incomplete type> “Incomplete type” is C terminology for data types that are not completely specified.


1 Answers

In C++ compiler support we can see that the defect report P1009R2:Array size deduction in new-expressions has not been adopted by all compiler vendors yet.

P1009R2 says: "The effect of the wording changes should be applied in implementations of all previous versions of C++ where they apply."

clang++ supports it from version 9 in C++11 mode and later.

like image 56
Ted Lyngmo Avatar answered Oct 27 '22 08:10

Ted Lyngmo