Here a piece of C++ code.
In this example, many code blocks look like constructor calls. Unfortunately, block code #3 is not (You can check it using https://godbolt.org/z/q3rsxn and https://cppinsights.io).
I think, it is an old C++ notation and it could explain the introduction of the new C++11 construction notation using {} (cf #4).
Do you have an explanation for T(i) meaning, so close to a constructor notation, but definitely so different?
struct T {
T() { }
T(int i) { }
};
int main() {
int i = 42;
{ // #1
T t(i); // new T named t using int ctor
}
{ // #2
T t = T(i); // new T named t using int ctor
}
{ // #3
T(i); // new T named i using default ctor
}
{ // #4
T{i}; // new T using int ctor (unnamed result)
}
{ // #5
T(2); // new T using int ctor (unnamed result)
}
}
Run Code Online (Sandbox Code Playgroud)
NB: thus, T(i) (#3) is equivalent to T i = T();
Bri*_*ian 13
The statement:
T(i);
Run Code Online (Sandbox Code Playgroud)
is equivalent to:
T i;
Run Code Online (Sandbox Code Playgroud)
In other words, it declares a variable named i with type T. This is because parentheses are allowed in declarations in some places (in order to change the binding of declarators) and since this statement can be parsed as a declaration, it is a declaration (even though it might make more sense as an expression).
| 归档时间: |
|
| 查看次数: |
269 次 |
| 最近记录: |