C++11 static_assert (and functions to be used therein)

前端 未结 2 658
说谎
说谎 2021-01-22 08:59

static_assert seems to be a very nice feature together with templates.

However, I have trouble finding functions in the standard library for doing various t

相关标签:
2条回答
  • 2021-01-22 09:23

    Take a look at the final C++11 draft, section 20.7, particularly the <type_traits> header.

    What you are asking is: std::is_base_of<base, derived>::value;

    Regarding your question: static_assert can be evaluated whenever the compiler sees fit, but it will usually:

    • In a template: if the expression uses dependent names, in instatiation time; else, in definition time.
    • Out of template: in definition time.
    0 讨论(0)
  • 2021-01-22 09:34

    In addition to @rodrigo’s answer (he was faster …),

    When is static assert executed? Can I put it anywhere in a template and it is evaluated for each template instanciation? Could it be used to constrain template parameters to be a specific subtype of a class?

    Unfortunately, no. For instance, a static_assert(false, "bummer"); is always executed, no matter the template. This in particular fails if you want to (partially) specialise a template.

    The standard (§7.4) says:

    [If the condition to static_assert is false] the program is ill-formed, and the resulting diagnostic message (1.4) shall include the text of the string-literal, […]

    Which is unfortunately quite unspecific but this lack of specificity is in fact exactly how static_assert behaves when it’s not dependent on a template type.

    You need to make the condition in a static_assert depend on the template argument to bind its execution to the particular template argument.

    So the following would fail:

    template <typename T>
    struct some_type {
        static_assert(false, "T must be a pointer type");
    };
    
    template <typename T>
    struct some_type<T*> {
        // …
    };
    

    Finally, I heartily recommend you read Marthino’s article on More type traits which details this process more, and gives hints on how to solve many trait-related problems elegantly.

    0 讨论(0)
提交回复
热议问题