2

I know that C & C++ are different languages standardized by different committees.

I know that like C efficiency has been a major design goal for C++ from the beginning. So, I think if any feature doesn't incur any runtime overhead & if it is efficient then it should be added into the language. The C99 standard has some very useful & efficient features and one of them is compound literals. I was reading about compiler literals here.

Following is a program that shows the use of compound literals.

#include <stdio.h>

// Structure to represent a 2D point
struct Point
{
   int x, y;
};

// Utility function to print a point
void printPoint(struct Point p)
{
   printf("%d, %d", p.x, p.y);
}

int main()
{
   // Calling printPoint() without creating any temporary
   // Point variable in main()
   printPoint((struct Point){2, 3});

   /*  Without compound literal, above statement would have
       been written as
       struct Point temp = {2, 3};
       printPoint(temp);  */

   return 0;
}

So, due to the use of compound literals there is no creation of an extra object of type struct Point as mentioned in the comments. So, isn't it efficient because it avoids the need for an extra operation copying objects? So, why does C++ still not support this useful feature? Are there any problems with compound literals?

I know that some compilers like g++ support compound literals as an extension but it usually leads to unportable code & that code isn't strictly standard conforming. Is there any proposal to add this feature to C++ also? If C++ doesn't support any feature of C there must be some reason behind it & I want to know that reason.

psmears
  • 26,070
  • 4
  • 40
  • 48
Destructor
  • 14,123
  • 11
  • 61
  • 126
  • 6
    `printPoint({2,3});` will work in C++ (>= C++11 I think) – Mat Feb 14 '16 at 10:31
  • @Mat: yes definitely. See http://melpon.org/wandbox/permlink/UfxN5SQ5HUQhuAIj . I think you are talking about new brace initializer syntax introduced by C++11. – Destructor Feb 14 '16 at 10:34
  • Who voted to close & Why? What's wrong in my question? – Destructor Feb 14 '16 at 10:34
  • @Mat: So, Can I say that due to {} initialization syntax there is no need of compound literals in C++? – Destructor Feb 14 '16 at 10:37
  • Because C++11 has {} initializers, you can have the pretty much the same syntax. And because in C++11 you can declare constructors `constexpr`, you can guarantee that the initialization may happen statically. – Petr Skocik Feb 14 '16 at 10:41
  • 4
    Voted to close because "why doesn't language X have this great must-have feature Y of language Z" questions are opinion-based and non-constructive. They sound like an attack on people who are in the business of creating language standards. Instead, ask "what feature similar to Y in language Z does language X provide?" – n. m. could be an AI Feb 14 '16 at 10:47

1 Answers1

7

I think that there is no need for compound literals in C++, because in some way, this functionality is already covered by its OOP capabilities (objects, constructors and so on).

You program may be simply rewritten in C++ as:

#include <cstdio>

struct Point
{
    Point(int x, int y) : x(x), y(y) {}
    int x, y; 
};

void printPoint(Point p)
{
    std::printf("%d, %d", p.x, p.y);
}

int main()
{
    printPoint(Point(2, 3)); // passing an anonymous object
}
psmears
  • 26,070
  • 4
  • 40
  • 48
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137