Chaos IN C ++ :: is the type? Or is it a function call?

zhaozj2021-02-16  56

Difficulty:

Text reminder: When you look at this article, your parents must accompany and guide, so as not to go to the magic.

Let's take a look at the code below? Struct a {a () {}}; template void func (t ()) {} int main () {a a (a ()); // (1), OK FUNC (A ()) ; // (2), WRONG A = 5; // (3), Wrong} (1), A A ()); what does it mean? Here is A () to create an object, then initialize the object a? Here, the true semantics are A is a function of a parameter to returns a parameter function pointer to the A object, so its true face should be A A (a (*) ()), where A () is a function type, in actual work The parameter is degraded into a A (*) () pointer. And this sentence only plays the role of the declaration function. The focus is that A () is not an object, but a function type (2), FUNC (A ()); FUNC (A ()); why is it wrong? Although the parameters of the function template FUNC are also a function type, why is it wrong? In fact, A () is not a function type, but an anonymous object. Then this function is Void Func (A), and the original function template FUNC type is Void (T (*) ()), which is clearly not matching the parameter type. (3), a = 5; there should be no doubt that INT is passed to A () (a (*)), and the earth people know that it is wrong. Don't walk around! What caused (1) and (2) A () showed different semantics? A: A () here has an amphibian: it can be interpreted as a temporary object of the newly created class A (to call a constructor in the process), or explain it as a function type declaration : The return value is an object of a type A, and the function parameters are empty. If it is the foregoing explanation, then is an object of class A; if it is the latter, the entire sentence is a function declaration: declares a function a, its return value type A, function parameters (omitted the parameter name. This is the above function type for a function declaration. In the end, the previous or last explanation depends on its semantic environment. More struct a {a (int) {}; a a (a (1)); here A (1) is impossible to be "type", because there is a 1, and A (1) is A conversion function call form, so A (1) here is called A (int). I am very grateful for whyglinux, pointing out the error and make changes to this article. // the end

转载请注明原文地址:https://www.9cbs.com/read-19341.html

New Post(0)