Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the C++ standard allow using a typedef to rename a constructor?

I was surprised to find that in VC++ 10, you can use a typedef to change the name of a class's constructor:

#include <iostream>

using namespace std;

class A
{
private:
    typedef A alias;

public:
    alias() { cout << "A ctor" << endl; }
};

int main()
{
    A(); // prints "A ctor"
    return 0;
}

Is this standard C++ or a Microsoft extension?

like image 368
Ferruccio Avatar asked Jan 04 '12 04:01

Ferruccio


People also ask

What should be the name of constructor in C?

For explanation: Constructor name should be same as the class name.

Can we refer to constructor address?

It is therefore not realistic to create a base class object using the derived class constructor function. We cannot refer to the addresses of constructors since we only require the address of a function to call it and they can never be called directly.


1 Answers

No; constructors do not have a name. You cannot take the address of a constructor or pass a function pointer around, or even just call it like a normal function. The syntax A::A() is just a special declarator syntax that allows you to declare and define the constructors, but it isn't a name.

That said, you cannot typedef objects (including function pointers) anyway, only types.

To comment on the MSVC behaviour, I quote from 12.1/3:

A typedef-name shall not be used [...] for a constructor declaration.

like image 89
Kerrek SB Avatar answered Oct 09 '22 21:10

Kerrek SB