std :: is_class在引用类上为false

问题描述 投票:0回答:1

标题几乎说了什么。

[std :: is_class为什么在引用上测试时为假

int main() {
    struct foo_struct {
        int i1;
        int i2;
    };
    std::cout << std::boolalpha << std::is_class<foo_struct>::value << std::endl; // true
    std::cout << std::boolalpha << std::is_class<foo_struct&>::value << std::endl; // falae
}
c++ c++17
1个回答
3
投票

引用类型和类类型是不同的类型;引用类型本身不是类类型。

作为type classification

C ++类型系统由以下类型组成:

您可以在类型上应用std::remove_reference,它为非引用类型提供类型本身。因此,您可以在模板中将其用于引用或非引用类型。

std::remove_reference

std::cout << std::boolalpha << std::is_class<std::remove_reference_t<foo_struct>>::value << std::endl; // true std::cout << std::boolalpha << std::is_class<std::remove_reference_t<foo_struct&>>::value << std::endl; // true

© www.soinside.com 2019 - 2024. All rights reserved.