MGA*_*MGA 5 c++ literals user-defined-literals constexpr c++11
我正在尝试使用C++自定义文字.我发现奇怪的是,当我将类型从long double类型更改为double或者尝试通过引用传递时,下面的简单函数停止工作.
起初我认为它与使用有关,constexpr但似乎并非如此,因为如果它们不在,这两种工作都很好operator "",并且constexpr从中operator ""删除不会删除错误.
这些经过深思熟虑的决策是在语言设计中,还是仅仅是我的编译器(gcc 4.8.2)无法处理的细微差别?
// Conversion function, works fine with both long double and
// double, and with or without pass by reference.
constexpr long double to_deg (const long double& deg)
{
return deg*M_PI/180.0;
}
// Custom literal with long double types and pass by value,
// works fine.
constexpr long double operator "" _deg (long double deg)
{
return deg*M_PI/180.0;
}
// Custom literal with double types, gives "Invalid argument list."
// error.
constexpr double operator "" _deg (double deg)
{
return deg*M_PI/180.0;
}
// Custom literal with pass by reference, gives "Invalid argument list."
// error. Removing the const does not remove the error.
constexpr long double operator "" _deg (const long double& deg)
{
return deg*M_PI/180.0;
}
Run Code Online (Sandbox Code Playgroud)
C++标准的第13.5.8节(用户定义的文字)列出了所有有效类型:
文字运算符的声明应具有等效于以下之一的参数声明子句:
- const char*
- unsigned long long int
- 长双
- 烧焦
- wchar_t的
- char16_t
- char32_t
- const char*,std :: size_t
- const wchar_t*,std :: size_t
- const char16_t*,std :: size_t
- const char32_t*,std :: size_t
无论是double或double&,const long double&在这里列出:所以他们是不允许的.
| 归档时间: |
|
| 查看次数: |
447 次 |
| 最近记录: |