问题描述
我已经看到,为了检查类型 T
是否是我可以使用的类:
I've seen that in order to check if a type T
is a class I can use:
bool isClass = std::is_class<T>::value;
对于类和结构均返回true。我知道在C ++中它们几乎是同一回事,但是我想知道为什么在类型特征中它们之间没有区别。
It returns true for both classes and structs. I know that in C++ they are almost the same thing, but I'd like to know why there's not a distinction between them in the type trait. Is it always useless to check this difference, or is there some more reason that I don't understand?
推荐答案
不幸的是,这是C ++中的常见误解。有时是由于基本的误解造成的,但有时是由于英语方面的歧义造成的。它可能来自不正确的编译器诊断,写得不好的书,不正确的SO答案…
Unfortunately this is a common misconception in C++. Sometimes it comes from fundamental misunderstanding, but at other times it comes from an ambiguity in English. It can come from inaccurate compiler diagnostics, badly-written books, incorrect SO answers…
您可能已经读过以下内容:
You've probably read something like this:
除了成员和基础的默认可见性之外,结构和类之间在C ++中没有区别。
"There is no difference in C++ between a struct and a class except the default visibility of members and bases."
这一段可以解释为这种误导性的感觉是因为在使用无差异之类的短语时,很难区分 identity 和平等的概念。
This passage can be interpreted in a sense that is misleading, because the notions of identity and equality are hard to distinguish when using phrases like "no difference".
实际上,C ++自1985年以来就没有结构。它只有类。
。期。保留关键字 struct
和使用该关键字定义类时默认的可见性规则,仅是为了与C…向后兼容。但这是语法问题。
The kind of types that you declare with the keyword class
and the keyword struct
are classes. Period. The keyword struct
, and the visibility rules that are the default when defining a class using that keyword, were kept only for backward compatibility with C … but that's a syntax thing. It doesn't make the resulting types actually be of a different kind.
类型特征没有区别,因为实际上没有一个类型。
The type trait makes no distinction because there literally isn't one to make.
这篇关于为什么没有std :: is_struct类型特征?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!