5

I recently learnt that constructors do not have names in C++ and some other things about them. I am also aware that a function has a type in C++ called a function type. For example,

void func(int)
{
}

In the above snippet the func has the function type void (int).

Now, I want to know that since constructors are special member functions then do they also have a type like the one shown above. For example say we have:

struct Name
{ 
    Name(int)
    {
    }
};

Does the constructor shown above also has a function type just like ordinary functions or ordinary member functions. If yes, then how can we find that type. Like we can use decltype on ordinary functions, is it permitted to use decltype on constructors to find their type.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • you don't call constructors (except in placement new situations), don't declare them outside the context of an object and can't take its address so there's no need to get its type – phuclv Apr 03 '22 at 10:23

2 Answers2

7

is it permitted to use decltype on constructors to find their type

It's not permitted. Primarily because there is no way to name a constructor. A common misnomer is that an expression like Name(0) or new Name(0), calls the constructor. But that isn't the case like in func(0). A constructor is never called by us directly, but rather always indirectly by the language construct that requires a new object to come into being.

[class.ctor.general]

1 ... Constructors do not have names.

2 A constructor is used to initialize objects of its class type. Because constructors do not have names, they are never found during name lookup; however an explicit type conversion using the functional notation ([expr.type.conv]) will cause a constructor to be called to initialize an object. [Note 1: The syntax looks like an explicit call of the constructor. — end note]

Because we cannot name them, we cannot use introspection mechanisms like decltype to examine them. Therefore the standard doesn't specify a "type" for constructors, since there is no way for a strictly standard compliant program to examine said type.

A constructor also cannot possess a signature (as defined by the standard), since that by definition includes the function name (and constructors are, as mentioned, nameless).

[defns.signature.member] signature

⟨class member function⟩ name, parameter-type-list, class of which the function is a member, cv-qualifiers (if any), ref-qualifier (if any), and trailing requires-clause (if any)

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • In the last statement that you [quoted](https://timsong-cpp.github.io/cppwp/n4868/defns.signature.member), why isn't return type mentioned there? Is the return type included in `trailing` part? I mean doesn't the signature of an ordinary function include the return type also? – Jason Apr 03 '22 at 10:26
  • @Mike https://stackoverflow.com/questions/290038/is-the-return-type-part-of-the-function-signature Free and member functions cannot be differentiated based on return type. Only (member) function template specializations can be. – user17732522 Apr 03 '22 at 10:29
  • 1
    A tiny bone to pick: you can't examine the types of member functions too, but they're specified. – Passer By Apr 03 '22 at 10:39
  • @PasserBy - You can derive other examinable types from them, though. A pointer to member function will carry the entire abominable function type of a member function. And of course, the entire process of forming a pointer to member requires naming the function. So I don't feel like I'm misdirecting too much here. – StoryTeller - Unslander Monica Apr 03 '22 at 11:39
-3

A classes constructor is never called explicitly. You instantiate an object using something like new Name(5), memory will be allocated and then some part of that memory may be initialized by the steps defined in the constructors body. Notice that a constructor has no return statement. What is returned by new Name(5) is a memory reference to the memory allocated by new.

This is given away by syntax like:

Name * foo = new Name(5)

foo is the pointer to whatever has be allocated and type checking can be done because Name referrs to a class, not to its constructor.

  • This does not my answer. I knew all this already. See [this](https://stackoverflow.com/questions/71680595/is-a-constructor-a-function-and-is-it-possible-to-call-a-constructor/71680636#71680636). – Jason Apr 03 '22 at 10:36
  • Then I don't understand your question. – Finkle MacGraw Apr 03 '22 at 10:45
  • 2
    @FinkleMacGraw Normal functions in C++ have a (function) type. E.g. in `void f();`, the function `f` has type `void()`. The question is asking whether a constructor also has a type. – user17732522 Apr 03 '22 at 10:48