在很多语言中,如C#和D,1f是一种声明浮点文字的有效方法,但在C++中,默认情况下最接近的是1.f或1.0f.但是,可以使用C++ 11的用户定义的文字运算符来实现此行为(即使它涉及打破"用户定义的文字的下划线"规则).下面的程序(至少在g ++ 4.9.2中)按预期工作,所以我想知道1f在C++中默认情况下浮点文字的有效语法是否有正当理由.
#include <iostream>
#include <typeinfo>
constexpr float operator""f(unsigned long long int i){
return static_cast<float>(i);
}
int main(){
auto f1 = 1f;
auto f2 = 1.f;
if(typeid(f1) == typeid(f2))
std::cout << "They're the same" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
134 次 |
| 最近记录: |