简体   繁体   中英

Should integer values be passed to floating point user defined literals?

I'm playing around with user-defined-literals (with GCC 4.7).

double operator"" _lb(long double n)
{
    return n * 0.453592; // convert pounds to kilos
}

This works fine when passing it a floating point literal (eg 42.0_lb ) however when I try to pass an integer literal (eg 42_lb ) I get the following error:

error: unable to find numeric literal operator 'operator"" _lb'

Shouldn't my definition of _lb cause an implicit conversion between the parameter to long double (as it would with regular functions)?

No, such implicit conversion doesn't apply in this case. As per [lex.ext]p3, a user-defined-intergal-literal can only be processed by a literal operator taking unsigned long long or by a raw literal operator (that takes const char* ) or a literal operator template.

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