Deduce template parameter of class - c++

Can someone please help me to understand why the following code does not compile:
#include <type_traits>
template< typename T >
class A
{};
template< typename T >
class B
{};
template< template <typename T> class GENERAL_t, // Note: GENERAL_t is either A<T> or B<T>
typename = std::enable_if_t< std::is_same<T,int>::value >
>
void foo( GENERAL_t a )
{}
error message:
t.cpp:67:57: error: use of undeclared identifier 'T'
typename = std::enable_if_t< std::is_same<T,int>::value >
^
t.cpp:67:65: error: no type named 'value' in the global namespace
typename = std::enable_if_t< std::is_same<T,int>::value >
~~^
t.cpp:69:15: error: use of class template 'GENERAL_t' requires template arguments
void foo( GENERAL_t a )
^
t.cpp:66:43: note: template is declared here
template< template <typename T> class GENERAL_t, // Note: GENERAL_t is either A<T> or B<T>
~~~~~~~~~~~~~~~~~~~~~ ^
3 errors generated.
Here, foo should take instances of class A or class B but only when the template argument T of A or B is an int.

you haven't declared T. It needs to be a template parameter.
drop the T in the template <class T> class GENERAL_t
GENERAL_t is a template template and as such requires a template parameter.
please don't use ALL_CAPS for anything except macros
This is the working code:
template<class T, template <class> class General_t,
class = std::enable_if_t< std::is_same<T,int>::value >
>
void foo(General_t<T> a)
{}

bolov's answer is correct in all respects.
But in this case, you don't need the SFINAE on is_same. You can just template on the template-template:
template <template <class> class General>
void foo(General<int> ) { }

Related

How to check if an object is an instance of a template class of multiple template arguments in C++?

I have the following class:
template <typename T, typename U = UDefault>
class A;
How to check whether types such as A<float> or A<float, int> are instances of the above templated class?
I tried modifying How to check if an object is an instance of a template class in C++? into:
template <typename T, typename U>
struct IsA : std::false_type
{};
template <typename T, typename U>
struct IsA<A<T, U>> : std::true_type
{};
but it's giving the following error
20:18: error: wrong number of template arguments (1, should be 2)
16:8: error: provided for 'template<class T, class U> struct IsA'
In function 'int main()':
40:34: error: wrong number of template arguments (1, should be 2)
16:8: error: provided for 'template<class T, class U> struct IsA'
How can this be solved?
Your IsA class should be expected to take one template argument. The type you are testing.
template <typename Type>
struct IsA : std::false_type {};
template <typename T, typename U>
struct IsA< A<T,U> > : std::true_type {};
// ^^^^^^ The specialization of your one template argument.
Or put alternately, since the individual template parameters to A do not matter here:
template <typename ...AParams>
struct IsA< A<AParams...> > : std::true_type {};
// ^^^^^^^^^^^^^ The specialization of your one template argument.
See it work in Compiler Explorer

Using CRTP with partial class specialization?

I'm trying to mix-in an operator [] with a class. My problem is I've partially specialized the class, and the compiler doesn't like me not specifying the template parameters to the derived class:
#include <iostream>
#include <type_traits>
using namespace std;
template <typename T>
struct mixin {
template <typename U>
void operator[](U u) {
cout << u;
}
};
template <typename T, typename = void>
struct derived : mixin<derived> {};
template <typename T>
struct derived<T,
typename enable_if<
is_same<T, int>{}
>::type> : mixin<derived> {};
int main() {
derived<int> d;
d[3.14];
}
With clang this gives:
test.cc:16:24: error: use of class template 'derived' requires template arguments
struct derived : mixin<derived> {};
^~~~~~~
test.cc:16:8: note: template is declared here
struct derived : mixin<derived> {};
^
test.cc:23:22: error: use of class template 'derived' requires template arguments
>::type> : mixin<derived> {};
^~~~~~~
test.cc:16:8: note: template is declared here
struct derived : mixin<derived> {};
^
gcc is even less helpful:
test.cc:16:31: error: type/value mismatch at argument 1 in template parameter list for ‘template<class T> struct mixin’
struct derived : mixin<derived> {};
^
test.cc:16:31: note: expected a type, got ‘derived’
test.cc:23:29: error: type/value mismatch at argument 1 in template parameter list for ‘template<class T> struct mixin’
>::type> : mixin<derived> {};
^
test.cc:23:29: note: expected a type, got ‘derived’
test.cc: In function ‘int main()’:
Is my only option to re-specify the template parameters inside of the mixin clause?
Well, try this:
#include <iostream>
#include <type_traits>
using namespace std;
template <typename T>
struct mixin {
template <typename U>
void operator[](U u) {
cout << u;
}
};
template <typename T, typename = void>
struct derived : mixin<derived<T>> {};
template <typename T>
struct derived<T,
typename enable_if<
is_same<T, int>::value
>::type> : mixin<derived<T>> {};
int main() {
derived<int> d;
d[3.14];
}
It does work...
What I changed:
Using is_same<foo,bar>::value, not is_same<foo,bar>{} edit: Hmm, it seems you don't need to change that after all. Neat!
Not trying to get the compiler to deduce the template parameter for derived when using mixin<derived>. You were being way too optimistic there...

Getting template dependent template type in C++

Well, I'm writing code in C++98 and trying to make use of metaprogramming.
I have written this kind of 'binder':
template<
template<
template<typename TArg1_1> class TArg1,
template<typename TArg2_1> class TArg2,
template<typename TArg3_1> class TArg3,
typename TArg4> class TFunction,
template<typename TArg1_1> class TArg1,
template<typename TArg2_1> class TArg2,
template<typename TArg3_1> class TArg3>
struct MetaBinder_4_3_templates_1_1_1_0 {
template <typename TArg4>
struct type : public TFunction<TArg1, TArg2, TArg3, TArg4> {};
};
What am I trying to do next is to 'bind' the arguments to it with the following code:
template<
template <typename TType> class TApplier,
typename TInitialType>
struct PreserveQualifiers : public PreserveQualifier<
TypeClearVolatile,
TypeSetVolatile,
typename template <typename> MetaBinder_4_3_templates_1_1_1_0 <
PreserveQualifier,
TypeClearConst,
TypeSetConst,
TApplier>::type,
TInitialType>
{};
PreserveQualifier is declared as follows:
template<
template <typename TType> class TClearer,
template <typename TType> class TSetter,
template <typename TType> class TApplier,
typename TInitialType>
struct PreserveQualifier;
But the code above actually gives me this error (GCC 4.7, my splitting into lines):
error: wrong number of template arguments (3, should be 4)
error: provided for '
template<
template<class TType> class TClearer,
template<class TType> class TSetter,
template<class TType> class TApplier,
class TInitialType
> struct PreserveQualifier'
I assume the problem is in typename template <typename> MetaBinder_4_3_templates_1_1_1_0 <...>::type construction syntax. And the actual question is: how should I specify dependent template type in this case?
Try:
MetaBinder_4_3_templates_1_1_1_0 <...>::template type
See a simplified example: LIVE DEMO

Use enable_if with is_integral to make distribution traits

I want to make a traits for std::uniform_*_distribution according to type given. E.g.:
distribution_traits<float>::type int_dist;
I tried following ways, but none of them compiles, and I don't know why.
Implementation 1
Use std::enable_if with typedefs:
template <typename T>
struct distribution_traits {
using type = typename std::enable_if<std::is_integral<T>::value, std::uniform_int_distribution<T>>::type;
using type = typename std::enable_if<std::is_floating_point<T>::value, std::uniform_real_distribution<T>>::type;
};
Clang 3.4 complains:
dist_traits.cpp:7:9: error: redefinition of 'type'
using type = typename std::enable_if<std::is_floating_point<T>::value, std::uniform_real_distribution<T>>::type;
^
dist_traits.cpp:6:9: note: previous definition is here
using type = typename std::enable_if<std::is_integral<T>::value, std::uniform_int_distribution<T>>::type;
^
dist_traits.cpp:6:40: error: no type named 'type' in 'std::enable_if<false, std::uniform_int_distribution<float> >'; 'enable_if' cannot be used to
disable this declaration
using type = typename std::enable_if<std::is_integral<T>::value, std::uniform_int_distribution<T>>::type;
^~~~~~~~~~~~~~~~~~~~~~~~~~
dist_traits.cpp:28:3: note: in instantiation of template class 'distribution_traits<float>' requested here
distribution_traits<float>::type int_dist;
^
2 errors generated.
Implementation 2
Use enable_if as class template parameter:
template <typename T, typename distribution_t = void>
struct distribution_traits;
template <typename T>
struct distribution_traits<
T, typename std::enable_if<std::is_integral<T>::value,
std::uniform_int_distribution<T> >::type > {
using type = std::uniform_int_distribution<T>;
};
template <typename T>
struct distribution_traits<
T, typename std::enable_if<std::is_floating_point<T>::value,
std::uniform_real_distribution<T> >::type > {
using type = std::uniform_real_distribution<T>;
};
And Clang complains
dist_traits.cpp:28:3: error: implicit instantiation of undefined template 'distribution_traits<float, void>'
distribution_traits<float>::type int_dist;
^
Either way cannot be compiled by MSVC++ 12.0, and the error messages are similar.
Could anyone please explain what's wrong I'm doing with SFINAE? Thanks!
For those who are curious about solution, here is the one that compiles:
template <typename T>
auto dist() -> typename std::enable_if<std::is_integral<T>::value, std::uniform_int_distribution<T>>::type;
template <typename T>
auto dist() -> typename std::enable_if<std::is_floating_point<T>::value, std::uniform_real_distribution<T>>::type;
template <typename T>
struct distribution_traits {
using type = decltype(dist<T>());
};
BTW, if put dist function into distribution_traits, the compilation will fail with error: function only differs in return type cannot be overloaded. :(
SFINAE can be used to discard overloads of function templates and class template specializations during substitution of template arguments.
It cannot be used with type/template aliases like you're trying to do.
About your working code - putting dist inside the class doesn't work because you attempt to call dist inside decltype without an object. Make dist static and it'll work:
template <typename T>
struct distribution_traits {
template <typename U>
static auto dist() -> typename std::enable_if<std::is_integral<U>::value, std::uniform_int_distribution<U>>::type;
template <typename U>
static auto dist() -> typename std::enable_if<std::is_floating_point<U>::value, std::uniform_real_distribution<U>>::type;
using type = decltype(dist<T>());
};
For implementation 2 to work, you need to omit the second argument of enable_if:
template
struct distribution_traits;
template <typename T>
struct distribution_traits<
T, typename std::enable_if<std::is_integral<T>::value>::type> {
using type = std::uniform_int_distribution<T>;
};
otherwise the specialization you define is distribution_traits<T, uniform_int_distribution<T>> and that doesn't match an instantiation like distribution_traits<float> because the second parameter is defaulted to void.

partial class template specialization with multiple arguments

I have a template class Foo that takes two (or more) template arguments. I want to use its type in a separate class Bar. See the following simple example, which compiles without error:
template <typename T, typename U> class Foo { };
template <typename T, typename U> class Bar { };
int main()
{
Bar<int, char> bar; // quick example -- int & char could be any 2 types
return 0;
}
The above is somewhat tedious, especially if Foo takes many template arguments and the programmer has to retype them all. I would like to have something like the following instead, but it does not compile:
template <typename T, typename U> class Foo { };
template <typename T, typename U> class Bar; // base
template <typename T, typename U> class Bar< Foo<T, U> > { }; // specialization
int main()
{
typedef Foo<int, char> FooType;
Bar<FooType> bar;
return 0;
}
test.cpp:3:60: error: wrong number of template arguments (1, should be 2)
test.cpp:2:45: error: provided for ‘template class Bar’
test.cpp: In function ‘int main()’:
test.cpp:7:18: error: wrong number of template arguments (1, should be 2)
test.cpp:2:45: error: provided for ‘template class Bar’
test.cpp:7:23: error: invalid type in declaration before ‘;’ token
I am especially perplexed because this partial specialization idiom works fine for a single template argument; see the question titled: total class specialization for a template
Edit I realized that, at least for my purposes, I could get around this using C++11 variadic templates as follows. I still want to know why the second example doesn't work, though.
template <typename... FooTypes> class Bar;
template <typename... FooTypes> class Bar< Foo<FooTypes...> > { };
Your class template Bar<T, U> takes two template arguments, but your specialization is only given one:
template <typename T, typename U> class Bar<Foo<T, U> > {};
Did you mean to have Bar take just one template argument and specialize it correspondingly?
template <typename T> class Bar;
template <typename T, typename U> class Bar<Foo<T, U> > {};
Note that a specialization can depend on a different number of template parameters but the specialization needs to get the same number of arguments. It also works the other way around: a full specialization can have no template parameter:
template <> class Bar<int> {};
I'm a little confused about what you're trying to do in this line:
template <typename T, typename U> class Bar< Foo<T, U> > { }; // specialization
You're stating that the template requires two types, T and U, as type parameters. Foo is itself only one type though, the type created by instantiating the Foo template with those two arguments.
I see that you're expecting it to pick up and determine the T and U since you used them in both places, but that doesn't circumvent the fact that you only provided one type argument for a two type template specialization.