Some usual template specialization like this:
template<class T>
class C
{
void common() { ... }
void f2 = delete;
};
template<>
class C<int>
{
void common() { ... }
void f1() { ... }
};
Could be represented with static_if
as:
template<class T>
class C
{
void common() { ... }
static_if(std::is_same<T, int>::value)
{
void f1( ) { ... }
}
else
{
void f2( ) = delete;
}
}
Are these directly competing features? Can template specialization do something static_if
cannot? It seems like static_if
can do everything template specialization can do, and much much more.
As an aside: I don't really like static_if
in this context because it potentially makes what parts of an interface are available to you in any given circumstance non-obvious. Maybe template specialization still provides clearer syntax in some cases.
An explicit specialization of a function template is inline only if it is declared with the inline specifier (or defined as deleted), it doesn't matter if the primary template is inline.
The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.
Explanation: explicit specialization is another name of full specialization.
One thing that static if
won't do for you is the "primary" way of using template specializations -- providing generic behaviour in one place and letting the users of your code override (= specialize) it for their specific needs/data types/etc...
No, static_if will not deprecate explicit template specialization. Explicit template specialization is a more powerful feature than static_if, providing many capabilities static_if isn't intended to. static_if is simply a more convenient and readable way to express certain things.
static_if can't do certain things explicit template specialization can, such as changing the the base classes a class inherits from.
struct S {};
template<typename T>
struct T
static_if(is_same<T,int>::value) { : S } // ?
{ };
template<typename T>
struct T {};
template<>
struct T<int> : S {};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With