Legal function parameters

While working with C ++ I started to wonder how the type code works sizeof(int)

. I realize this sizeof

is an operator, not a function, but I'm still wondering ... is this code like myFunc(double)

legal? Can you only pass the pure type of something, for example int

or MyClass

to a function? Admittedly, I can't see many apps for this, but I'm just curious.

+3


source to share


2 answers


Operators such as sizeof

work because the language says what they do.

Not all of this is construct, like a function that you can overload or recreate yourself. You cannot make your own operator that takes a type name, just like you cannot create your own primitive type or declaration syntax.



You cannot overwrite it sizeof

in your C ++ program and use it there (although its base implementation in the compiler is often written in C ++!), But at a different level of abstraction).

There is no magic here; just the facts of life.

+2


source


No, you cannot. Arguments are expression , and in C ++ types are not expressions.



Templates are used for "type arguments", but they are only used at compile time. (See @Banex and @clcto, above.)

+3


source







All Articles