-1

Is there a way to create a custom "std::initializer_list" class that is defined using brackets "[]" instead of curly brackets?

Code example.

class BracketsInitializerList { 
    // class definition here
    // ...
};
class SomeClass {
public:
    SomeClass(BracketsInitializerList<int> x) { ... }
};
int main() {
      SomeClass x = [0, 1, 2, 3, 4, 5];
}
  • No, the standard says the syntax use `{}` not `[]`. – Jason Jul 06 '22 at 15:43
  • 2
    Why you'd want to do that? – paolo Jul 06 '22 at 15:43
  • @AnoopRana Yeah that is what I am hoping. – user3134709 Jul 06 '22 at 15:44
  • @paolo - Just for python like c++ library. – user3134709 Jul 06 '22 at 15:44
  • `SomeClass x = [0, 1, 2, 3, 4, 5];` isn't legal syntax in any context at all, even ignoring all the semantics/types. – user17732522 Jul 06 '22 at 15:45
  • @user17732522 Ofcourse, yet that it not the question. The question is if it is possible to make this legal syntax with code in some way? – user3134709 Jul 06 '22 at 15:46
  • Warning: If you code in C++ the way you code in Python, you will be discarding many of the things that make C++ worth using. C++ requires different thinking than Python. You don't want to forget everything you learned from Python, but you do need to learn different ways of applying it to get the outcome you desire. – user4581301 Jul 06 '22 at 15:46
  • @user3134709 See *"You can't intercept the parsing of a braced-init-list"* [here](https://stackoverflow.com/questions/72886429/c-is-there-a-way-to-create-a-custom-stdinitializer-list-class-that-is-de?noredirect=1) – Jason Jul 06 '22 at 15:47
  • @user3134709 No, I mean that fundamentally when you want to parse C++ code, the C++ grammar doesn't allow that construction. So that directly implies that you can't make it work, no matter what you try. – user17732522 Jul 06 '22 at 15:47
  • **Yes**, there is a way. But it requires you to make your own language. And that language won't be C++. – Eljay Jul 06 '22 at 15:47
  • @user4581301 You are totally right. Its just that i created a recursive dictionary and array class with universal types. And it would be very nice if i could define an array with brackets, in some way. – user3134709 Jul 06 '22 at 15:47
  • @Eljay And how would I do that? Could i still be using g++ or a C compiler? Or should I then build a compiler myself? because I was also thinking about editing the code before running it through the compiler to support decorators types just like in python. And also something like \@constcopy that copies the function with a const tag. – user3134709 Jul 06 '22 at 15:48
  • You could use Boost [Spirit X3](https://www.boost.org/doc/libs/master/libs/spirit/doc/x3/html/index.html) to define your own grammar. Spirit X3 has an EBNF-like syntax using C++ as a eDSL. – Eljay Jul 06 '22 at 15:51
  • @AnoopRana I don't see how that is a good duplicate. The question here is specifically _not_ about a braced-init-list and `std::initializer_list`'s behavior. – user17732522 Jul 06 '22 at 15:51
  • @Eljay I see thanks, But would it be worth it to edit the code before running it through the gcc compiler, using string slicing etc. Or is this a little strange? – user3134709 Jul 06 '22 at 15:53
  • No stranger than Bjarne's idea "C with Classes" that used a front-end to transpile *C with Classes* code into **C** code. – Eljay Jul 06 '22 at 15:56
  • @user17732522 One of the reason i mentioned that dupe is because the answer here just talks about "compiler magic"(when there is no such thing) without explaining anything what they mean by that and moreover this question(and answer given here also) has worse quality than that one. But mainly because there the answer specifically says: *"**You can't intercept the parsing of a braced-init-list**"*. – Jason Jul 06 '22 at 15:56
  • @user17732522 • Spirit X3 is the eDSL, with it you can create your own grammar to process text. That text can be in the format that the OP wants. Spirit X3 could be used to create an entire programming language, with the interpreting engine being done in C++. – Eljay Jul 06 '22 at 16:16
  • @Eljay Oh, I misread your comment. – user17732522 Jul 06 '22 at 16:18

1 Answers1

3

No. You can't even create a custom replacement for std::initializer_list (that uses {...}), since it uses compiler magic.


But you can create an imitation: create a global variable with an overloaded operator[] (which can accept multiple parameters in C++23), and do my_var[1, 2, 3].

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207