[webkit-dev] How we enable template functions

Keith Miller keith_miller at apple.com
Tue Aug 22 20:34:53 PDT 2017


Hello fellow WebKittens,

I’ve noticed over time that we don’t have standard way that we enable versions of template functions/classes (flasses?). For the most part it seems that people use std::enable_if, although, it seems like it is attached to every possible place in the function/class.

I propose that we choose a standard way to conditionally enable a template.

There are a ton of options; my personal favorite is to add the following macro:

#define ENABLE_TEMPLATE_IF(condition) static_assert(condition, “template disabled”)

Then have every function do:

template<typename T>
void foo(…)
{
    ENABLE_TEMPLATE_IF(std::is_same<T, int>::value);
    …
}

And classes:

template<typename T>
class Foo {
    ENABLE_TEMPLATE_IF(std::is_same<T, int>::value);
};

I like this proposal because it doesn’t obstruct the signature/declaration of the function/class but it’s still obvious when the class is enabled. Obviously, I think we should require that this macro is the first line of the function or class for visibility. Does anyone else have thoughts or ideas?

Cheers,
Keith

P.S. in case you are wondering why this macro works (ugh C++), it’s because if there is any compile time error in a template it cannot be selected as the final candidate. In my examples, if you provided a type other than int foo/Foo could not be selected because the static_assert condition would be false, which is a compile error.


More information about the webkit-dev mailing list