Parameter Matching and Enumerations
Because an object of enum
type may be initialized only by another object of that enum
type or by one of its enumerators (§ 19.3, p. 833), an integral value that happens to have the same value as an enumerator cannot be used to call a function expecting an enum
argument:
// unscoped enumeration; the underlying type is machine dependentenum Tokens {INLINE = 128, VIRTUAL = 129};void ff(Tokens);void ff(int);int main() { Tokens curTok = INLINE; ff(128); // exactly matches ff(int) ff(INLINE); // exactly matches ff(Tokens) ff(curTok); // exactly matches ff(Tokens) return 0;}
Although we cannot pass an integral value to an enum
parameter, we can pass an object or enumerator of an unscoped enumeration ...
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.