C++ function template overload on template parameter - c++

Should it be possible to overload function template like this (only on template parameter using enable_if):
template <class T, class = std::enable_if_t<std::is_arithmetic<T>::value>>
void fn(T t)
{
}
template <class T, class = std::enable_if_t<!std::is_arithmetic<T>::value>>
void fn(T t)
{
}
if conditions in enable_if don't overlap? My MSVS compiler complains, that 'void fn(T)' : function template has already been defined. If not, what is the alternative (ideally not putting enable_if anywhere else than into template parameters)?

Default arguments don't play a role in determining uniqueness of functions. So what the compiler sees is that you're defining two functions like:
template <class T, class>
void fn(T t) { }
template <class T, class>
void fn(T t) { }
That's redefining the same function, hence the error. What you can do instead is make the enable_if itself a template non-type parameter:
template <class T, std::enable_if_t<std::is_arithmetic<T>::value, int> = 0>
void fn(T t) { }
template <class T, std::enable_if_t<!std::is_arithmetic<T>::value, int> = 0>
void fn(T t) { }
And now we have different signatures, hence different functions. SFINAE will take care of removing one or the other from the overload set as expected.

Related

Specializing a template method with enable_if

I'm writing a template class that stores a std::function in order to call it later. Here is the simplifed code:
template <typename T>
struct Test
{
void call(T type)
{
function(type);
}
std::function<void(T)> function;
};
The problem is that this template does not compile for the void type because
void call(void type)
becomes undefined.
Specializing it for the void type doesn't alleviate the problem because
template <>
void Test<void>::call(void)
{
function();
}
is still incompatible with the declaration of call(T Type).
So, using the new features of C++ 11, I tried std::enable_if:
typename std::enable_if_t<std::is_void_v<T>, void> call()
{
function();
}
typename std::enable_if_t<!std::is_void_v<T>, void> call(T type)
{
function(type);
}
but it does not compile with Visual Studio:
error C2039: 'type' : is not a member of 'std::enable_if'
How would you tackle this problem?
Specialize the whole class:
template <>
struct Test<void>
{
void call()
{
function();
}
std::function<void()> function;
};
SFINAE doesn't work over (only) the template parameters of the class/structs.
Works over template methods whit conditions involving template parameters of the method.
So you have to write
template <typename U = T>
std::enable_if_t<std::is_void<U>::value> call()
{ function(); }
template <typename U = T>
std::enable_if_t<!std::is_void<U>::value> call(T type)
{ function(type); }
or, if you want to be sure that U is T
template <typename U = T>
std::enable_if_t< std::is_same<U, T>::value
&& std::is_void<U>::value> call()
{ function(); }
template <typename U = T>
std::enable_if_t<std::is_same<U, T>::value
&& !std::is_void<U>::value> call(T type)
{ function(type); }
p.s.: std::enable_if_t is a type so doesn't require typename before.
p.s.2: you tagged C++11 but your example use std::enable_if_t, that is C++14, and std::is_void_v, that is C++17
If you don't stick to use void, and your intention is to actually able to use Test without any parameters, then use variadic template:
template <typename ...T>
struct Test
{
void call(T ...type)
{
function(type...);
}
std::function<void(T...)> function;
};
This way, you can have any number of parameters. If you want to have no parameters, use this:
Test<> t;
t.call();
So this is not exactly the syntax you wanted, but there is no need for specialization, and this solution is more flexible, because supports any number of parameters.

Are require-clauses evaluated after parameter substitution inside declarations?

In the C++ standard, N4618/[temp.deduct] (§14.8.2), the following example (§14.8.2/7) demonstrates how template parameter substitution is performed in lexical order:
template <class T> struct A { using X = typename T::X; };
template <class T> typename T::X f(typename A<T>::X);
template <class T> void f(...) { }
template <class T> auto g(typename A<T>::X) -> typename T::X;
template <class T> void g(...) { }
void h() {
f<int>(0);// OK, substituting return type causes deduction to fail
g<int>(0);// error, substituting parameter type instantiates A<int>
}
I was expecting requires-clauses content to be also evaluated before declaration content. I expected that the following modification would not generate any compilation error:
template <class T> struct A { using X = typename T::X; };
template <class T> typename T::X f(typename A<T>::X);
template <class T> void f(...) { }
template <class T>
requires false
auto g(typename A<T>::X) -> typename T::X;
template <class T>
requires true
void g(...) { }
void h() {
f<int>(0);// OK, substituting return type causes deduction to fail
g<int>(0);// error, substituting parameter type instantiates A<int>
}
Actually GCC tell me there is still an error. Is it the behaviour stated in the concept TS?
Yes. See the note in N4630 [temp.deduct]/5:
The satisfaction of constraints (14.10) associated with the function
template specialization is determined during overload resolution
(13.3), and not at the point of substitution.

template template parameter for stl containers behaving different from custom template class

I have the following struct and function
template <class T> struct C {};
template <template <class S> class T, class U> void f() { T<U> tu; }
when templating f() with C I do not get an error, when templating it with say std::vector I do.
int main() {
f<C, int>();
}
yields no errors
int main() {
f<std::vector, int>();
}
yields
error: no matching function for call to 'f'
f<std::vector, int>();
^~~~~~~~~~~~~~~~~~~~~~~~
note: candidate template ignored: invalid explicitly-specified argument for template parameter 'T'
template <template <class S> class T, class U> void f() { T<U> tu; }
What is the difference between C and std::vector here?
That's because vector has two template parameters, not one (T and Allocator).
You can either change your f template to accept two template parameters (or a variadic pack):
template <template <class...> class T, class U> void f() { T<U> tu; }
or you can alias vector to a 1-parameter template:
template<typename T>
using vec = std::vector<T>;
difference is that vector has two template parameters, not one. To fix this you may use
template <template <class... S> class T, class U> void f() { T<U> tu; }

Member function template selection and SFINAE

I've been trying to understand the way C++ selects templates. Namely, consider the following code sample:
template <typename R>
class Curious
{
public:
template <typename T, typename std::enable_if<std::is_const<T>::value, int>::type = 33>
void test1() {}
template <typename T, typename std::enable_if<!std::is_const<T>::value, int>::type = 33>
void test1() {}
template <typename T, typename = typename std::enable_if<std::is_const<T>::value>::type>
void test2() {}
template <typename T, typename = typename std::enable_if<!std::is_const<T>::value>::type>
void test2() {}
template <typename std::enable_if<std::is_const<R>::value>::type * = nullptr>
void test3() {}
template <typename std::enable_if<!std::is_const<R>::value>::type * = nullptr>
void test3() {}
// works
template <typename T = void>
typename std::enable_if<std::is_const<R>::value, T>::type test4() {}
template <typename T = void>
typename std::enable_if<!std::is_const<R>::value, T>::type test4() {}
// also works
template <typename T = void, typename std::enable_if<std::is_const<R>::value, T>::type * = nullptr>
void test5() {}
template <typename T = void, typename std::enable_if<!std::is_const<R>::value, T>::type * = nullptr>
void test5() {}
}; // Curious
The first two functions (test1) work fine (why?):
Curious<int> curious;
curious.test1<int>();
curious.test1<const int>();
While the rest of them cause compilation errors.
Regarding the function test2 the compiler claims I'm trying to create a duplicate:
error C2535: 'void Curious::test2(void)': member function already defined or declared
Here the documentation says:
A common mistake is to declare two function templates that differ only
in their default template arguments. This is illegal because default
template arguments are not part of function template's signature, and
declaring two different function templates with the same signature is
illegal.
So it seems to be the case. However, I don't see that much difference from the first two functions, which also have the default template argument. Thus we have a default type (test2 - doesn't work) against a default value (test1 - works). Is there any rule about it?
In case of test3: error C2039: 'type': is not a member of 'std::enable_if'
Like in the first case this time the member function template has a default non-type parameter, but it depends on the class template parameter. Now SFINAE doesn't skip the wrong one (also not sure why).
In the fourth case SFINAE resolves the template by the return type. But don't these test4 functions have identical signature? As they differ only in the return type.
As far as I understand, in the fifth case adding extra parameter makes test5 signature dependent on the function template parameter, therefore SFINAE kicks in and resolution works.
I'm quite confused about how C++ deals with these templates. Could somebody be so kind to clear these things up?
With default value removed, for test1, you have:
template <typename T, typename std::enable_if<std::is_const<T>::value, int>::type>
void test1();
template <typename T, typename std::enable_if<!std::is_const<T>::value, int>::type>
void test1();
Which have clearly different signatures.
For test2:
template <typename T, typename> void test2();
template <typename T, typename> void test2();
Which are clearly identical signatures.
For test3, SFINAE doesn't apply as you have hard error as R is fixed in the class and your enable_if doesn't depend of template parameter of the function.
For test4, there is an exception about signature for template function as overload may differ only by return type so
int foo();
char foo(); // Illegal.
but
template <typename T> int foo();
template <typename T> char foo(); // legal, even it is not trivial to call
In addition, std::enable_if<!std::is_const<R>::value, T>::type depends on template parameter T so it is ok.
For test5, second template parameter depends on first template parameter T, so it is ok too.

Function templates: Different specializations with type traits

Considering class templates, it is possible to provide template specializations for certain types of groups using type traits and dummy enabler template parameters. I've already asked that earlier.
Now, I need the same thing for function templates: I.e., I have a template function and want a specialization for a group of types, for example, all types that are a subtype of a class X. I can express this with type traits like this:
std::enable_if<std::is_base_of<X, T>::value>::type
I thought about doing it this way:
template <typename T, typename ENABLE = void>
void foo(){
//Do something
}
template <typename T>
void foo<T,std::enable_if<std::is_base_of<A, T>::value>::type>(){
//Do something different
}
However, this does not work since partial specialization is not allowed for function templates. So how to do it then? Maybe a default parameter with the type trait as type? But how does the code look like then?
Overloads:
void foo_impl(T, std::false_type);
void foo_impl(T, std::true_type);
foo(T t) { foo_impl(t, std::is_base_of<A, T>()); }
The closest to what you're asking is enable_if on the return type:
template<typename T> typename std::enable_if<std::is_same<T, int>::value>::type foo();
template<typename T> typename std::enable_if<std::is_same<T, char>::value>::type foo();
However, dispatching to a helper function or class is likely to be more readable and efficient.
Helper function:
template<typename T> void foo_helper(std::true_type);
template<typename T> void foo_helper(std::false_type);
template<typename T> void foo() { foo_helper(std::is_same<T, int>()); }
Helper class:
template<typename T, bool = std::is_same<T, int>::value> struct foo_helper {};
template<typename T> struct foo_helper<T, true> { static void foo(); };
template<typename T> struct foo_helper<T, false> { static void foo(); };
template<typename T> void foo() { foo_helper<T>::foo(); }
Do the actual implementation (partial specializations etc..) in class templates and write a small wrapper template function that does nothing but call a static function in your class templates.
Tried a few things and finally came up with the correct syntax myself - sorry for asking. I didn't know that enable_if has a second parameter. By using this parameter and a default value, it is possible.
Here is the answer
template<typename T>
void foo(typename std::enable_if<std::is_base_of<A, T>::value,int>::type ENABLER = 0){
std::cout << "T is a subclass of A!";
}
template<typename T>
void foo(typename std::enable_if<!std::is_base_of<A, T>::value,int>::type ENABLER = 0){
std::cout << "T is NOT a subclass of A";
}