3

I want to know what it means for a function to return a class/structure name followed by parentheses.

For example:

struct X{...}; //structure with static members

X g(){ return X(); }; 

I found this in the following link: https://en.cppreference.com/w/cpp/language/static#Explanation

JeJo
  • 30,635
  • 6
  • 49
  • 88

1 Answers1

1
X() 

calls the default-constructor of struct X.

return X();

means construct a temporary instance of X and return it. More specifically, here the temporary is a pure r-value which will be moved to the lvalue where the function is being called.

that is

auto objectX = g();

By the above statement, the objectX(lvalue) will be initialized by moving the temporary created from the g().

See more about the value categories in C++ here: https://en.cppreference.com/w/cpp/language/value_category

JeJo
  • 30,635
  • 6
  • 49
  • 88
  • Does it mean that `X()` returns a value (object) even if it's a constructor that should return nothing by definition? Is it just a special syntax for constructors? – Hermis14 Feb 19 '23 at 04:16