简体   繁体   中英

Can I make string literal into identifier

In Visual C++, given this function declaration:

int main();

In function body, I want to define a varible which has a type same as function's (return) type, but suppose I don't want to use either int or main, so I tried something like this:

int main() {
    decltype(__identifier(__FUNCTION__)) x;
}

This doesn't work because __identifier doesn't accept string literal. Can I use some method to make string literal into identifier? Or, is there a way we can define x of type int within function body, without use of return type (int) or function name (main)?

Actually, it does accept string literals, but you have to suppress a warning for it to work. You can do this:

#define SYMBOL_FROM_STRING(S) (__pragma(warning(suppress: 4483)) __identifier(S))

decltype(SYMBOL_FROM_STRING(__FUNCTION__)) x;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM