7

The code below:

struct Foo
{
    Foo(int){} // no default constructor
};

int main()
{
    Foo* pFoo = new Foo[2]{1,2}; // OK in g++, fails in clang++!
    delete[] pFoo;
}   

compiles in gcc, but fails in clang. Is the code above syntactically correct?

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Neither does wandbox. http://melpon.org/wandbox/permlink/jS7mx3XTG2xjDqb9 – Baum mit Augen May 20 '16 at 01:04
  • 3
    Just when I thought we could trust our tools. I think you broke one of them. See, this is why we can't have nice compilers. – wally May 20 '16 at 01:23
  • If you are using C++11, switch to `std::vector` and use its constructor that accepts a *braced-init-list* as input: `std::vector pFoo = {1, 2};` or maybe `std::vector pFoo = {Foo(1), Foo(2)};` – Remy Lebeau May 20 '16 at 01:37
  • @RemyLebeau Yes, I know that works. I wondered whether the "classic" C++ code is correct. I bumped into it when answering [this](http://stackoverflow.com/a/37336250/3093378). – vsoftco May 20 '16 at 01:38
  • @vsoftco: In C++03, you can only default-initialize the array items when using `new[]`. In C++11, you can initialize the individual items using specific values by using the brace syntax you showed. My guess is clang fails if it is trying to terminate the brace-init-list with a default-constructed sentry object or something like that, whereas gcc is not. Sounds like a clang bug, since the syntax itself is fine. – Remy Lebeau May 20 '16 at 01:44
  • 1
    The code is syntactically correct: `new Foo[2]` may be followed by a brace-enclosed list. I guess you are actually asking whether it is well-formed – M.M May 20 '16 at 01:57
  • @M.M No, was actually asking about the syntax, as clang failed to compile it and I observed in general that clang tends to be more standard compliant. So this made me wonder... What do you mean by "well formed"? – vsoftco May 20 '16 at 01:59
  • @vsoftco well, it is syntactically correct. clang does not give a syntax error, it says "no matching constructor". – M.M May 20 '16 at 02:00
  • @M.M Ohh I see what you mean, thanks, yes, that's what I meant. – vsoftco May 20 '16 at 02:00
  • 1
    Looks like clang bug to me. The error messages from clang 3.8 are nonsense, e.g. "note: in implicit initialization of array element 2 with omitted initializer" – M.M May 20 '16 at 02:08
  • 1
    If you do actually provide a default constructor, clang doesn't call it – M.M May 20 '16 at 02:08
  • This looks like serious bug in `clang++` to me !!! – Destructor May 20 '16 at 03:13
  • This program also compiles fine on VC++ 2015 see http://webcompiler.cloudapp.net/ – Destructor May 20 '16 at 03:15
  • [this](http://en.cppreference.com/w/cpp/language/new) says that _If initializer is a brace-enclosed list of arguments, the object is list-initialized_ . – Destructor May 20 '16 at 03:22
  • @Destructor Which confirms that the code should be accepted (C++11 of course). I guess it's a bug in clang. – vsoftco May 20 '16 at 03:24
  • @vsoftco: definitely. You should immediately submit bug report to clang !!! – Destructor May 20 '16 at 03:24
  • @vsoftco: behaviour of **clang++** seems so inconsistent. It allows initialization of an objects with _automatic storage duration_ . See live demo [here](http://coliru.stacked-crooked.com/a/da6071dea8c19ff7) . – Destructor May 20 '16 at 03:33
  • @Destructor Filled: https://llvm.org/bugs/show_bug.cgi?id=27821. And yes, I know, automatic list-initialized objects are fine. – vsoftco May 20 '16 at 03:34
  • 4
    See https://llvm.org/bugs/show_bug.cgi?id=22924 and the discussion therein; also http://wg21.link/cwg2102. – T.C. May 20 '16 at 04:26

1 Answers1

0

I'll turn @T.C. comment into an answer, so everyone sees what's going on without having to read through all of the comments.

This is a clang bug, the code should be accepted: http://llvm.org/bugs/show_bug.cgi?id=22924

Related: http://wg21.link/cwg2102

vsoftco
  • 55,410
  • 12
  • 139
  • 252