typename 注意事项

在使用typename时, 大都数情况下的用法是template <typename T>, 很容易忽略下面的用法:

#include <iostream>
#include <type_traits>

class T {
    public:
        using inner_t = int;
};

template <typename T>
class Any {
    public:
        using underlying_t = typename T::inner_t;
};

int main(void) {
    const Any<T>::underlying_t a = 10;
    static_assert(a == 10, "failed");
}

当我们需要调用类型 T 内部成员时, 需要带上 typename. 如果觉得有帮助,可以扫描右边的微信打赏码支持一下.

Leave a Reply

Your email address will not be published. Required fields are marked *