c++ - 使用不带 typedef 的 <ratio>

标签 c++ c++11 typedef

我总是被告知 typedef用于简化事物或缩短名称。我的意思是,我们可以在没有它的情况下做事。但是今天我尝试使用 <ratio>库,我不知道如何不使用 typedef 来做事.

这是一个简单的例子:

#include <iostream>
#include <ratio>    // C++11

int main()
{
    const int   a = 1, b = 17;
    typedef     std::ratio<a,b> p;

    std::cout << "Number p = " << p::num << "/" << p::den << std::endl; 

    return 0;
}

如何在不使用 typedef 的情况下做同样的事情? ?

最佳答案

我不明白你为什么需要 typedef:

int main() {
    const int a = 1, b = 17;
    std::ratio<a,b> p;

    std::cout << "Number p = " << p.num << "/" << p.den << std::endl; 

    return 0;
}

或者,如果您不想创建变量,请按照其他人提到的那样进行操作:

std::cout << "Number p = " << std::ratio<a,b>::num << "/" << std::ratio<a,b>::den << std::endl; 

关于c++ - 使用不带 typedef 的 <ratio>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23807672/

相关文章:

c++ - Visual Studio C++ 枚举需要很长时间才能编译

c++ - 资源管理器获取图像的缩略图非常快,它是如何工作的?

c++ - 在命名空间中声明变量,在 main 中定义它,使其对所有其他文件可见

c++ - 涉及 "using"别名的成员函数重载解析中的英特尔 C++ 编译器错误?

c++ - 已删除 "general"案例的专用模板函数无法使用 g++ <=4.8.0 和 clang++ 编译

c++ - '.Position' 的左边必须有类/结构/union

c++ - 在定义 ClassName 之前采用参数 ClassName * 的 typedef 函数指针?

c++ - 如何转换为私有(private)派生的子类型?

c++ - 迭代冒泡排序

检查我对指针/结构和数组的理解