0undefined
0undefined

Reputation: 68

Square brackets — not array related?

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?

Upvotes: 3

Views: 148

Answers (3)

trid-zah
trid-zah

Reputation: 11

It looks like C++/CLI code, may be some code from .NET?

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234715

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.

Upvotes: 4

Walter
Walter

Reputation: 45434

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);

Upvotes: 5

Related Questions