Reputation: 7867
In my impression it seems I saw something like that:
A:::b()
But I don't remember if it is c++,also after searched "triple colon" in c++ but seems rare information about it in c++, is there any example that the code can have 3 colons(:::) in the code which is valid syntax?
Upvotes: 3
Views: 2854
Reputation: 301
This is slightly off topic because it refers to inline assembly rather than C++ per se. However, here's a tip for those who got here (as I did) after seeing this operator in the following expression:
asm volatile("" ::: "memory");
This syntax is used by gcc to tell the compiler to create a 'memory barrier'. Wikipedia's Memory Ordering topic is a better explanation than I can provide.
Upvotes: 16
Reputation: 241871
Yes, there are valid C++ programs which include the sequence of characters :::
(outside of comments and quoted literals).
Normally, that sequence would be lexically analysed as a ::
token (the scope resolution operator) followed by a :
token. As far as I know, there is no valid C program of this form, because a :
can only be preceded by a name (as a label) or an expression (either in a case
label or as part of the ternary ?:
operator). And ::
cannot be the last token in an expression.
You can force the lexical analyser to produce a :
followed by a ::
but only by placing whitespace between the two tokens, making : ::
. And, of course, you can use the preprocessor to define a macro which ignores or stringifies an argument, so :::
could appear as an argument to such a macro.
Leaving those aside, as far as I know the only possibility is when the first :
is the second character in a <:
token. For example:
const int size = 42;
int A<:::size:>;
If that looks weird, remember that <:
is an alternative spelling for [
and :>
is an alternative spelling for ]
. The unary scope resolution operator ::
indicates that the name it qualifies is in global scope.
Curiously, <::
is an exception to the maximal-munch rule if it is not followed by :
or >
, in which case it must be analysed as <
followed by ::
rather than as <:
followed by :
. However, when it is followed by :
(as above) or >
, maximal munch still applies and the first token is <:
.
Upvotes: 4
Reputation: 61970
Given a global function b
:
int b() {
return 5;
}
There are two (actually a couple more per Steephen's answer if you get creative1) cases where this would work, but only with a space:
As a label:
int main() {
A: ::b();
}
As part of a conditional expression:
int main() {
int A = 0;
int result = true ? A: ::b();
}
The reason the space is needed is because C++ lexing is greedy and always lexes :::
as ::
followed by :
, never :
followed by ::
. (A notable exception to this is >>
being allowed to end two template parameter/argument lists only since a special change C++11.)
1: See examples of access modifiers, inheritance, constructor initializer list.
Upvotes: 5
Reputation: 15824
No, there is no triple colon(:::) as operator in C++
There is scope resolution operator, it is double colon(::).
There is single colon(:) that you can see along with access specifiers like private, public and protected. And also within constructor syntax
Upvotes: 0