Search code examples
c++g++standardstypedefstandards-compliance

Is it legal C++ to use a typedef in a method declaration but the canonical type in the method definition?


The GNU C++ (g++ -pedantic -Wall) accepts this:

typedef int MyInt;

class Test
{
public:
    MyInt foo();
    void bar(MyInt baz);
}; 

int Test::foo()
{
    return 10;
}

void Test::bar(int baz)
{
}

int main(void)
{
    Test t;
    t.bar(t.foo());
    return 0;
}

Is it legal C++? Are other compilers likely to accept it?


Solution

  • Yes it is legal:

    7.1.3 The typedef specifier

    A name declared with the typedef specifier becomes a typedef-name. Within the scope of its declaration, a typedef-name is syntactically equivalent to a keyword and names the type associated with the identifier in the way described in clause 8. A typedef-name is thus a synonym for another type. A typedef-name does not introduce a new type the way a class declaration (9.1) or enum declaration does.