Why is this short code snippet valid?

According to the heading, I do not understand how the following code can compile when has_type_struct<no_type>, of course, is 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;
}
+4
source share
1 answer

The program is valid because has_type_struct<no_type>it is not created.

[temp.inst] / 1 :

If the specialization of the class template has not been explicitly installed or explicitly specialized, the specialization of the class template is implicitly created when the specialization is referenced in a context that requires a fully defined type of object or when the completeness of the class type affects the semantics of the program.

my_int<has_type_struct<no_type>> has_type_struct<no_type>, .

+2

Source: https://habr.com/ru/post/1677446/


All Articles