14

Like in this example (in C):

typedef int type;

int main()
{
    char type;
    printf("sizeof(type) == %zu\n", sizeof(type)); // Outputs 1
}

The output is always the size of the local variable type.

When C++ removed the need to write struct before each use of a structure it still preserved the struct {type} syntax and introduced an alias (class {type}) to explicitly refer to a structure or class.

Example (in C++):

struct type {
    int m;
};

int main()
{
    char type;
    printf("sizeof(type) == %u\n", sizeof(type)); // Outputs 1
    printf("sizeof(struct type) == %u\n", sizeof(struct type)); // Outputs 4
    printf("sizeof(class type) == %u\n", sizeof(class type)); // Outputs 4
}

My question is if there is a way to explicitly refer to a typedef in C or C++. Something like sizeof(typedef type) perhaps (but that does not work).

I know that it is common practice to use different naming conventions for variables and types to avoid these kinds of situations but I would still like to know if there is a way within the langauge to do this or if there is not. :)

wefwefa3
  • 3,542
  • 2
  • 24
  • 50
  • 2
    Your file-scope typedef and your block-scope `char` object have the same name. The fix is to rename one of them so you can refer to them unambiguously. (`type` is a poor name for a type anyway, unless it actually represents a type, e.g. in the code that implements a compiler or interpreter.) – Keith Thompson Nov 26 '14 at 16:03
  • this will not cleanly compile due to this line: char type; which becomes char int; which will either raise a alias or a masking warning. In any case, the printf will always use the local variable 'type' – user3629249 Nov 27 '14 at 05:09

3 Answers3

9

There is no way to resolve this one but if your structure is defined globally you can use this,

Scope resolution operator ::.

printf("sizeof(type) == %zu\n", sizeof(::type));
Venkatesh
  • 1,487
  • 14
  • 26
  • @AlexFarber: Yes, I just noticed that. So this question is completely about scope. – Ben Voigt Nov 26 '14 at 15:45
  • @Venkatesh: Yes, you can. But not both a typedef and a variable. However this is legal: `int main() { struct type {}; int type; }` – Ben Voigt Nov 26 '14 at 15:48
4

In C this is not possible. You are hiding the type type. You cannot use it as a type after you declare the char:

typedef int type;

int main(void) {
    char type;
    type t;      // error: expected ‘;’ before ‘t'
    printf( "%d %d\n", sizeof type, sizeof t );
    return 0;
}

However, if you create an alias for type or declare a type before you declare the char you can use that:

int main(void) {
    type t;
    char type;
    printf( "%d %d\n", sizeof type, sizeof t );
    return 0;
}


int main(void) {
    typedef type type_t;
    char type;
    printf( "%d %d\n", sizeof type, sizeof( type_t ) );
    return 0;
}

C++ has the scope resolution operator :: which you can use to reference the type using the qualified name, i.e. ::type or my_namespace::type.

clcto
  • 9,157
  • 17
  • 39
  • I accept this answer because I think it is the best answer. It tells me how to refer to a `typedef` that is overshadowed and does not rely on `type` being in global scope. – wefwefa3 Nov 26 '14 at 19:08
3

In C++, use :: operator to get the answer as 4.

printf("sizeof(::type) == %u\n", sizeof(::type));

The :: is used for accessing global variables in C++. In C, there is no direct way i think. You can do it using functions.

The :: operator works even if it is not a class or struct.

typedef int type1;

int main() {
 int type1;
 cout<<sizeof(::type1);
 return 0;
}

This will also give the answer as 4.

shintoZ
  • 311
  • 2
  • 12