Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static_assert and class templates

I have a problem with the static_assert feature. When I instantiate a class template directly, everything works as expected. But when I pass it as a parameter for the different class template, static_assert does not work.

template <int X>
class A{
    static_assert(X == 0, "X != 0");
};

template <class T>
class B{

};

B<A<1>> b;           // Static assert does not work
A<1>    a;           // error: static assertion failed: X != 0

EDIT

Thanks all for the answers. Is there a way to explicitly instantiate A without creation of A instances / inheriting from A? I was trying this:

template <int X>
class A{
    static_assert(X == 0, "X != 0");
};

template <class T>
class B;

template <template <int X> class T, int X>
class B<T<X>>{
    template class T<X>;
};

But this is incorrect.

like image 956
valentin Avatar asked May 03 '17 13:05

valentin


People also ask

What is the difference between class and template?

An individual class defines how a group of objects can be constructed, while a class template defines how a group of classes can be generated. Note the distinction between the terms class template and template class: Class template.

What is a class template?

Class templates A class template provides a specification for generating classes based on parameters. Class templates are generally used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments.

Can template be used for class?

A template is not a class or a function. A template is a “pattern” that the compiler uses to generate a family of classes or functions. In order for the compiler to generate the code, it must see both the template definition (not just declaration) and the specific types/whatever used to “fill in” the template.

What is Static_assert?

What is static assertion? Static assertions are a way to check if a condition is true when the code is compiled. If it isn't, the compiler is required to issue an error message and stop the compiling process. The condition that needs to be checked is a constant expression. Performs compile-time assertion checking.


2 Answers

For B<A<1>> b;, A<1> is only used as template argument, which doesn't cause implicit instantiation of class template A, then the static_assert inside A's definition won't be triggered.

When code refers to a template in context that requires a completely defined type, or when the completeness of the type affects the code, and this particular type has not been explicitly instantiated, implicit instantiation occurs. For example, when an object of this type is constructed, but not when a pointer to this type is constructed.

On the other hand, for A<1> a;, A<1> is required to be a complete type (to construct a), then implicit instantiation happens, static_assert is fired.

EDIT

You can use sizeof (which requires the type to be complete) to cause the implicit instantiation and fire the static_assert. e.g.

template <class T>
class B{
    static_assert(sizeof(T) > 0, "static_assert for implicit instantiation");
};
like image 129
songyuanyao Avatar answered Nov 15 '22 12:11

songyuanyao


Your class template B doesn't do anything with its T, so its T isn't instantiated.

Thus "nothing happens" to the A<1> in B<A<1>>.

If you had a member T a inside B then you'd get the assertion failure.

like image 23
Lightness Races in Orbit Avatar answered Nov 15 '22 12:11

Lightness Races in Orbit