template <template <typename> class F> struct call_me {};
template <typename T> struct maybe;
template <typename... T> struct more;
int main()
{
call_me<maybe> a; // ok
call_me<more> b; // error
}
I understand why call_me<more> fails. But I want to make it work.
Is there a workaround that doesn't involve changing call_me (or add an specialization to it)?
template <template <typename> class F> struct call_me {};
template <typename T> struct maybe;
template <typename... T> struct more;
template <template <class...> class F> struct just_one {
template <class A> using tmpl = F<A>;
};
int main()
{
call_me<maybe> a;
call_me<just_one<more>::tmpl> b;
}
Not exactly equivalent, but maybe close enough.
template <typename T> using onemore = more<T>;
int main()
{
call_me<onemore> b;
}
You could wrap more:
template <template <typename...> class Tmpl>
struct variwrap
{
template <typename> struct Dummy
{
template <typename ...Brgs>
struct rebind
{
typedef Tmpl<Brgs...> other;
};
};
};
Now you can say call_me<variwrap<more>::Dummy>, and the consumer can use F::rebind<Args...>::other to recover more<Args...>. Of course call_me has no way of knowing that F has the rebind member, so you'll need to add a specialization.
Yuck.
Related
Suppose I have a class template
template<class T>
class Foo{};
Is it possible to prevent T from being an instantiation of Foo. That is, this should not compile:
struct Bar{};
Foo<Foo<Bar>> x;
Another option:
#include <type_traits>
template <typename T>
struct ValidFooArg;
template <typename T>
requires ValidFooArg<T>::value
class Foo
{
};
template <typename T>
struct ValidFooArg : std::true_type {};
template <typename T>
struct ValidFooArg<Foo<T>> : std::false_type {};
int main()
{
Foo<int> x; // ok
Foo<Foo<int>> y; // error
}
You might still provide partial specialization to have error in that case:
template <typename T>
constexpr bool always_false = false;
template<class T>
class Foo<Foo<T>>
{
static_assert(always_false<T>);
};
What I'm looking for is a way to say: This is the same for all specializations:
template <typename T>
struct Foo {
using id_type = unsigned int; // this does not depend on T!
};
Foo::id_type theId; // Doesn't matter what the specialization is, id_type is always the same.
I want to access id_type without having to specify the specialization...
You can't have exactly what you are asking for. Foo is not a class. Foo<T> is a class, for any T.
You could have a non-template base that holds id_type
struct FooBase {
using id_type = unsigned int;
};
template <typename T>
struct Foo : FooBase{};
FooBase::id_type theId;
You could provide a default parameter for T
template <typename T = struct FooPlaceholder>
struct Foo {
using id_type = unsigned int; // this does not depend on T!
};
Foo<>::id_type theId;
However nothing stops me from writing an explicit specialisation of Foo that lacks (or redefines) id_type.
template <> struct Foo<MyType> { };
template <> struct Foo<MyOtherType> { int id_type = 42; };
Instead of id_type being a class (alias declaration) property, you could make it a stand-alone trait on a template template parameter:
#include <type_traits>
// Helper: compare with std::is_same but for
// template template parameter type arguments.
template <template <typename> typename, template <typename> typename>
struct is_same_primary_template : std::false_type {};
template <template <typename> typename TT>
struct is_same_primary_template<TT, TT> : std::true_type {};
template <template <typename> typename TT, template <typename> typename UU>
constexpr bool is_same_primary_template_v{
is_same_primary_template<TT, UU>::value};
template <typename T> struct Foo {};
template <template <typename> typename, typename Enable = void> struct id_type;
template <template <typename> typename TT>
struct id_type<TT, std::enable_if_t<is_same_primary_template_v<TT, Foo>>> {
using type = int;
};
// ...
template <template <typename> typename TT>
using id_type_t = typename id_type<TT>::type;
int main() { id_type_t<Foo> theId; }
This approach has one drawback, though. As the trait is specialized over specific types it couples these types with the implementation of the trait (as you want be very careful with the location of your specializations, w.r.t. the location of the primary template).
When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.
Just an extension based on Caleth's answer and the comment ensuing from that.
You can kind of protect yourself from "bad" specializations of Foo like this:
#include <iostream>
#include <type_traits>
//-------------------------------------------------------------------------
// from Caleth
struct FooBase
{
using id_type = unsigned int;
};
template <typename T>
struct Foo : FooBase
{
};
FooBase::id_type theId{};
//-------------------------------------------------------------------------
// specialization bypassing FooBase
template<>
struct Foo<char>
{
};
//-------------------------------------------------------------------------
// compile time check if someone mad a "bad" specialization, pre C++20
template<typename T>
void f(const Foo<T>& foo)
{
static_assert(std::is_base_of_v<FooBase, Foo<T>>);
}
//-------------------------------------------------------------------------
// C++20 concept to check for FooBase
template<typename T>
concept HasFooBase = std::is_base_of_v<FooBase, T>;
// only accepts types derived from FooBase
void g(const HasFooBase auto& foo)
{
}
//-------------------------------------------------------------------------
int main()
{
Foo<int> foo;
Foo<char> bar;
f(foo);
g(foo);
f(bar); // won't compile, error C2607: static assertion failed
g(bar); // won't compile, error C7602: 'g': the associated constraints are not satisfied
return 0;
}
Suppose I have a struct
template <typename Args>
struct MyStruct
{
};
But I only want to be able to instantiate this class with std::tuple instantiations, e.g.
Mystruct<std::tuple<>> a; // OK
Mystruct<std::tuple<int, int, double>> a; // OK
Mystruct<double> a; // FAIL
How can I do this?
This is fairly simple. Declare but not define a general template:
template<typename T> struct Mystruct;
then define a specialization:
template<typename... Ts>
struct Mystruct<std::tuple<Ts...>>
{
// stuff
};
In addition to krzaq's answer, to have a better error message, you might want to use static_assert
// The traits:
template <typename T>
struct is_a_tuple : std::false_type {};
template <typename ... Ts>
struct is_a_tuple<std::tuple<Ts...>> : std::true_type {};
// Your structure (for error message)
template <typename T>
struct MyStruct
{
static_assert(is_a_tuple<T>::value, "T should be a std::tuple");
};
// Your structure (for std::tuple)
template <typename ... Ts>
struct MyStruct<std::tuple<Ts...>>
{
// Your implementation
};
I've some trouble to make use of template template parameters. Here is a very simplified example:
template <typename T>
struct Foo {
T t;
};
template <template <class X> class T>
struct Bar {
T<X> data;
X x;
};
int main()
{
Bar<Foo<int>> a;
}
The compiler (g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2) reports the following error:
main.cpp:8:5: error: ‘X’ was not declared in this scope
T<X> data;
^
main.cpp:8:6: error: template argument 1 is invalid
T<X> data;
^
Any idea what's wrong?
So I would like so make use of something like Bar<Foo<>>
template <typename T = int>
struct Foo {
T t;
};
template <typename T>
struct Baz {
T t;
};
template <typename T>
struct Bar;
template <template <typename> class T, typename X>
struct Bar<T<X>> {
T<X> data;
X x;
};
int main()
{
Bar<Foo<>> a;
Bar<Baz<float>> b;
}
template <typename T>
struct Foo {
T t;
};
template <template <class> class T, class X>
struct Bar {
T<X> data;
X x;
};
int main()
{
Bar<Foo, int> a;
}
In
template <template <class X> class T>
The template type parameter X is not a template parameter to the outermost template: it is a template parameter to the innermost template. It's rather similar to
int foo(int (*bar)(int x))
{
int y = x; // compiler error
}
which doesn't work since the function takes a single argument, bar: there is no argument x.
Depending upon what you are truly trying to do, you could add the second template parameter, with something like
template <typename X, template <typename> class T >
struct Bar
{
// ...
};
you can keep the declaration with a single type parameter, but pattern match to give a partial specialization that would define the class in the example context
template <typename T>
struct Bar;
template <typename X, template <typename> class T >
struct Bar<T<X>>
{
// ...
};
you could modify Foo to have a useful nested type, and grab it that way
template <typename T>
struct Bar
{
using X = T::value_type;
};
or you could define a metafunction that extracts a template parameter from a template type, and get it that way:
template <typename T>
struct Bar
{
using X = get_parameter<T>;
};
The most flexible is the last version, except rather than a metafunction that extracts template arguments, you would declare a get_bar_parameter function, and define a partial specialization that extracts the template parameter from a Foo<X> (or a T<X>). That way, if you ever decide in the future to use Bar with classes where the right value of X isn't computed that way, you can do so by giving an appropriate specialization for get_bar_parameter.
// method 1
template <typename T>
struct Foo {
typedef T Type;
T t;
};
template <typename T>
struct Bar {
T data;
typename T::Type x;
};
// method 2
template <typename T>
struct Hack
{
T t;
};
template <typename T>
struct TypeOf
{
typedef struct UnknownType Type;
};
template<>
struct TypeOf< Hack<int> >
{
typedef int Type;
};
template <typename T>
struct Baz {
T data;
typename TypeOf<T>::Type X;
};
int main()
{
Bar< Foo<int> > a;
Baz< Hack<int> > b;
return 0;
}
In method 1 information is provided with nested type. This requires changing original class.
In method 2 this information is provided with specialization of another template.
I have a question about templates and it is in the code:
template<typename T>
struct foo {
T t;
};
template<typename FooType>
struct bar {
T t; //<- how to get T here (preferably without using typedef in foo)
};
Here's a generic template argument type extractor:
#include <tuple>
template <typename> struct tuplify;
template <template <typename...> class Tpl, typename ...Args>
struct tuplify<Tpl<Args...>>
{
using type = std::tuple<Args...>;
};
template <typename T, unsigned int N>
using get_template_argument
= typename std::tuple_element<N, typename tuplify<T>::type>::type;
Usage:
get_template_argument<std::vector<int>, 1> a; // is a std::allocator<int>
Or in your case:
get_template_argument<FooType, 0> t;
If I understood your question correctly, you could use template specialization as follows. Given your foo<> class template:
template<typename T>
struct foo {
T t;
};
Define a bar<> primary template and a corresponding specialization this way:
template<typename FooType>
struct bar;
template<typename T>
struct bar<foo<T>> {
T t; // T will be int if the template argument is foo<int>
};
Under the assumption that you are always supposed to instantiate bar by providing an instance of foo<> as the type argument, you can leave the primary template undefined.
The specialization will match the foo<T> pattern, thus giving you the type with which foo<> is instantiated in T.
Here is how you could test the validity of this approach with a simple program:
#include <type_traits>
int main()
{
bar<foo<int>> b;
// This will not fire, proving T was correctly deduced to be int
static_assert(std::is_same<decltype(b.t), int>::value, "!");
}
Here is the corresponding live example.
If you don't want or can't add a typedef to foo, you can additionally write an independent "extractor" template
template <typename T> struct ExtractT;
template <typename T> struct ExtractT<foo<T> > {
typedef T type;
};
and use it as
template<typename FooType>
struct bar {
typename ExtractT<FooType>::type t;
};
You can take that ExtractT one step further and decouple it from foo
template <typename T> struct ExtractT;
template <template <typename> class C, typename T> struct ExtractT<C<T> > {
typedef T type;
};
and so on until you reinvent something from Boost or C++11 standard library :) BTW, this feels like something that should already be available in form of a more generic solution....