再来个 c++ 11 中的 user defined literal ,std::chrono中的 5s, 6min, 7h等都是这么实现的,不过用户实现的必须前面加下划线 ```c++ struct Weight { long double g = 0; void show() { cout << "Weight: " << g << " g" << endl; } }; Weight operator "" _kg(long double num) { return {num * 1000}; } int main() { auto w = 25.2_kg; w.show(); } ```
1 1

相关推荐

牛客网
牛客企业服务