Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Square brackets — not array related?

Tags:

c++

I know that hard brackets ('[' and ']') are used to identify an array, but whilst searching for how to use events I stumbled upon another use for them, and wonder what exactly it means...

The code I saw (link) looks like this:

// evh_native.cpp
#include <stdio.h>

[event_source(native)]
class CSource {
public:
   __event void MyEvent(int nValue);
};

[event_receiver(native)]
class CReceiver {
public:
    …

So...I wonder what exactly this means and what it is used for? Can someone please explain?

like image 473
0undefined Avatar asked Dec 18 '25 20:12

0undefined


2 Answers

Apart from the array usage and class operator[], there is another use in C++, the capture list of lambdas, like

int a;
auto func= [a](int i){ return i*a; }

but the example in your link looks like non-standard (it's referred to as native C++, whatever that is). Yet another use are double brackets for attributes, like

[[noreturn]] void throw_error(std::string const&message);
like image 162
Walter Avatar answered Dec 20 '25 09:12

Walter


It could be a few things; to me it looks like your case is the last on my list:

  1. To specify an index of an array.

  2. To specify the capture list in a lambda expression or function.

  3. [] can be overloaded in a way that it detaches completely from its original intention. Boost spirit does this.

  4. A non-standard compiler extension.

1, 2, and 3 can all be implemented in standard C++; 4 cannot.

like image 25
Bathsheba Avatar answered Dec 20 '25 11:12

Bathsheba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!