本文介绍了为什么这段简短的模板代码有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
按照标题,当has_type_struct<no_type>
肯定是无效类型时,我不理解下面的代码如何编译.
As per the title, I do not understand how can the following code compile when has_type_struct<no_type>
is certainly an invalid type.
template<typename T>
using my_int = int;
struct no_type {};
template<typename T>
struct has_type_struct { using type = typename T::type; };
template<typename T>
using has_type_using = typename T::type;
int main() {
my_int<has_type_struct<no_type>> a; // why does this compile?
//my_int<has_type_using<no_type>>(); // this rightfully does not compile
return 0;
}
推荐答案
该程序有效,因为未实例化has_type_struct<no_type>
.
The program is valid because has_type_struct<no_type>
is not instantiated.
my_int<has_type_struct<no_type>>
的使用不需要完成has_type_struct<no_type>
,因此不会实例化后者,并且不会检查其定义中从属名称的有效性.
The use of my_int<has_type_struct<no_type>>
does not require has_type_struct<no_type>
to be complete, therefore the latter is not instantiated and the validity of the dependent name in its definition is not checked.
这篇关于为什么这段简短的模板代码有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!