C++ Alias to Template? - c++

I'm trying to create an alias to a template, rather than a type and I can't find the syntax to do it. Below is an example that demonstrates my problem. My guess is this is just something that can't be done, but I'm hoping someone can prove me wrong. If it can't be done, is there some underlying reason it doesn't make sense to do this, or is it just not implemented?
template <class S>
class Down;
template <class S>
class Up {
template <class S1>
using Opposite = Down<S1>;
};
template <class S>
class Down {
template <class S1>
using Opposite = Up<S1>;
};
template <template <typename> class Direction>
void oneDirection() {
//Call another function here that uses the template argument as a template
}
template <template <typename> class Direction>
void bothDirections() {
oneDirection<Direction>();
oneDirection<Direction::Opposite>(); //This doesn't compile
}
int main() {
bothDirections<Up>();
}

In Direction::Opposite, Direction:: is a nested-name-specifier, and it can't indeed denote a class template (you'd need to give it the required template arguments to make it a template specialization).
I suppose one reason to not allow that form is that class templates can have partial or explicit specializations, which can provide different members from the primary template, so the compiler needs to work with a specific specialization to be able to know exactly what's available in there.
You can work around this by using traits to associate the two templates:
template<class> class Up { };
template<class> class Down { };
template<template<class> class Direction> struct Direction_traits;
template<> struct Direction_traits<Up>
{
template<class S1> using Opposite = Down<S1>;
};
template<> struct Direction_traits<Down>
{
template<class S1> using Opposite = Up<S1>;
};
template<template<class> class Direction>
void oneDirection() {
//Do something here
}
template<template<class> class Direction>
void bothDirections() {
oneDirection<Direction>();
oneDirection<Direction_traits<Direction>::template Opposite>();
}
int main() {
bothDirections<Up>();
}
However, keep in mind that Direction_traits<Up>::Opposite is not the same template as Down, at least not yet - the language rules may change in the future, more details in this answer and its comments.
This could cause problems if you want to get back to Up from inside oneDirection<Direction_traits<Up>::Opposite> using the traits - there won't be a trait specialization defined for the alias template. Things would need to get a bit more complicated to allow such use; a possible solution is outlined in the answer quoted above.

Related

Reducing duplication of template parameters in C++

This is not a major code breaking issue, I'm just wondering if I'm missing some neat trick.
If I am writing a templated class, I may start like this:
// some_header.h
template <typename TypeParameter, size_t max_array_size>
class TemplatedClass
{
std::array<TypeParameter, max_array_size> MyTemplatedArray;
public:
TypeParameter do_something()
{
/* do something with TypeParameter and max_array_sizein here */
}
}
This is fine, but when I have less trivial templated examples, I tend to separate out the function definitions from the declarations, like so:
// some_header.h
template <typename TypeParameter, size_t max_array_size>
class TemplatedClass
{
std::array<TypeParameter, max_array_size> MyTemplatedArray;
public:
TypeParameter do_something();
/*
Many more function declarations
*/
}
template <typename TypeParameter, size_t max_array_size>
TemplatedClass<TypeParameter, max_array_size>::do_something()
{
/* do something with TypeParameter and max_array_sizein here */
}
/*
Many more function definitions, all with:
template <typename TypeParameter, size_t max_array_size>
TemplatedClass<TypeParameter, max_array_size>
at the start
*/
The aim of this would be to have a classic skeleton class definition that can easily be read by others at a glance. I don't mind doing this, but the annoying thing is when I want to modify the template parameters. What is one change in the first example, ends up being 1 + 2 * n changes in the second example!
So what I am wanting to know is: Is there a way to make the second example's template parameters more maintainable? Maybe something similar to a typedef/using or maybe some keyword I haven't heard of?
First a non-answer:
Suppose, /*do something*/ depends on the template parameters. In that case, having to fix the signature is the smaller issue. You need to fix the implementation.
The other case is that /*do something*/ does not depend on the template parameters. Then you can move the methods to a non template base class.
A more serious attempt to answer the question:
What if TemplatedClass would have only a single template parameter instead of many? Instead of using a bunch of template parameters for TemplatedClass you can use a single tag and definition of the actual parameters can be defered to traits:
#include <array>
template <typename Tag> struct value_type_impl;
template <typename Tag> using value_type = typename value_type_impl<Tag>::type;
template <typename Tag> constexpr int array_size = 123;
template <typename Tag>
class TemplatedClass {
std::array<value_type<Tag>, array_size<Tag> > MyTemplatedArray;
public:
value_type<Tag> do_something();
};
template <typename Tag>
value_type<Tag> TemplatedClass<Tag>::do_something() {
return {};
}
// the tag
struct foo_tag{};
// specializations for foo_tag:
template <> struct value_type_impl<foo_tag> { using type = int; };
template <> constexpr int array_size<foo_tag> = 123;
int main() {
TemplatedClass<foo_tag> tc;
}
Now the burdon is on the user of the template to define one tag and then specialize the required traits for that tag. Though, the method definition has only a single template parameter that does not need to change when more "parameters" (traits) are added or removed. Of course you still need to fix the implementation.
If you were looking merely for some syntactic sugar, I am not aware of something that would help without changing the template itself.

Is there a way(in C++) to create a template class that implements certain functions?

I want to create a class that accepts only certain types of template classes. I know that there exists template specialization, but I want my class to accept all templates that implement a specific function, search.
Let's say I have a class A as follows:
template<class T> //add a restriction that T implements bool search(T)
class A
{
T t;
//do something that uses T.search(T x)
if(t.search(x))
//Do something
};
So basically, I want to create a generic class that works for all classes that have the search functionality. Is there a way to do this?
I want to create a generic class that works for all classes that have the search functionality. Is there a way to do this?
By example, using decltype() as follows
template <typename T,
typename = decltype(std::declval<T>().search(std::declval<T>()))>
class A
{
};
The following is a full compiling example for a size() enabled class A
#include <string>
#include <type_traits>
template <typename T, typename = decltype(std::declval<T>().size())>
class A
{
};
int main()
{
A<std::string> as;
//A<int> ai; // compilation error
}
There is a drawback in this solution: you can hijack it explicating the second template parameter; by example, the following code compile
A<int, void> ai;
To avoid this problem you can use partial specialization as follows
template <typename T,
typename = decltype(std::declval<T>().search(std::declval<T>()))>
class A;
template <typename T>
class A<T>
{
};

Passing template prototype as template argument - is it possible?

So first, apologies for terminology - I'm not sure if template prototype is the correct term. By this I mean :
template <class T, class X>
class TemplatePrototype
{
// code
};
I have a situation where I have a function that creates a template object based upon template arguments to that function.
template <class T, class X>
void doSomething()
{
TemplatePrototype<T, X> aTemplateTX;
aTemplateTX.doSomethingElse();
}
However, there are about 15 different versions of TemplatePrototype, which all have the same interface but different execution (TemplatePrototype is provided by another library). As a result, I have a lot of code that looks like this:
template <class T, class X>
void doSomethingWithOne()
{
TemplatePrototypeOne<T, X> aTemplateTX;
aTemplateTX.doSomethingElse();
}
template <class T, class X>
void doSomethingWithTwo()
{
TemplatePrototypeTwo<T, X> aTemplateTX;
aTemplateTX.doSomethingElse();
}
As a consequence of the architecture, I must know which TemplatePrototype I am going to use before I know the actual types T and X. I would like to see something like this:
template <class T, class X, class Prototype>
void doSomething()
{
Prototype<T, X> aPrototype;
aPrototype.doSomething();
}
But where I have specified part of the template arguments in advance - i.e I specify Prototype before I know T and X. Obviously, this is not possible in C++.
Equally, I cannot pass the Prototype as a template argument because it will still result in huge amounts of duplicate code.
Some important facts : I know the range of all possible inputs.
So I could theoretically use a macro to define each possible template specialisation and insert them into a container, which I would then use to access the specialisation I need. However, I am looking for a more 'elegant' solution - is it possible to pass template prototypes without specialising them as an argument to a template class, and then instantiate later when a function is called? Example:
template <class Prototype>
class Holder
{
template <class T, class X>
void doSomething()
{
Prototype<T, X> aPrototype;
aPrototype.doSomethingElse();
}
};
As far as I know this is impossible, but I was wondering if the SO community had some folks who know a solution?
EDIT:
So I have implemented this as my solution, thanks to the answers below!
#include <iostream>
template <typename T>
struct Foo
{
Foo() { aPtr = 0; }
T* aPtr;
};
template <template<typename> class C>
struct Bar
{
template <class T>
void doSomething()
{
C<T> aClass;
if (aClass.aPtr)
std::cout << "Hello world" << std::endl;
}
};
int main()
{
Bar<Foo> aFoo;
aFoo.doSomething<int>();
return 0;
}
This enables me to specify which TemplatePrototype I wish to use, before I can know the template parameters.
Yes, use a template template parameter, e.g.
template <typename T>
struct Foo
{
};
template <template<typename> class C>
struct Bar
{
};
then
Bar<Foo> b;
You're looking for template template parameters.
In the template parameter list, instead of just:
class TemplatePrototype
specify your prototype as a class template which itself has two template type parameters (without giving them a name here), like:
template<class,class> class TemplatePrototype
//^^^^^^^^^^^^^^^^^^^
This will result in a function like:
template <class T, class X,
template<class,class> class TemplatePrototype>
void doSomething()
{
TemplatePrototype<T, X> aTemplateTX;
aTemplateTX.doSomethingElse();
}
Invocation example:
doSomething<T, X, TemplatePrototypeOne>();
To become independent of the number of template parameters you pass to your "prototype" (here it was 2, namely T and X), you can use variadic templates (since C++11).
For this, first move the prototype template parameter to the first position:
template <template<class,class> class TemplatePrototype,
class T, class X>
Then, replace class T, class X with class ...Ts, which is a placeholder of an arbitrary number of type parameters. Also, in the template template parameter list, replace class,class with class.... And in the instantiation within the function implementation, replace <T, X> with <Ts...> to "expand" the parameter pack.
The result then looks like this:
template <template<class...> class TemplatePrototype,
class ... Ts>
void doSomething()
{
TemplatePrototype<Ts...> aTemplateTs;
aTemplateTs.doSomethingElse();
}
Live demo

Is there a way to bind a template<template> parameter?

Context
I have a custom comparator that takes another comparator and applies an additional check:
template <template <typename> class Comparator, typename T>
struct SoftOrder : public std::binary_function<T, T, bool> {
bool operator()(const T lhs, const T rhs) const {
return Comparator<T>()(lhs, rhs) && AnotherCheck();
}
};
I have a second class that accepts a comparator, e.g.:
template <template <typename> class Comparator>
class Processor { ... };
It is easy to instantiate a Processor with a standard comparator (e.g. std::less) like so:
Processor<std::less> processor1;
Processor<std::greater> processor2;
However it is not so easy to instantiate with SoftOrder as the compiler correctly complains about the missing second template argument:
Processor<SoftOrder<std::less> > processor3; // <-- Fails to compile
Current Solutions
I have come up with a few solutions prior to posting this question.
First Solution - Lots of Derived Classes
template <typename T>
struct SoftOrderLessThan : public SoftOrder<std::less, T> {};
template <typename T>
struct SoftOrderGreaterThan : public SoftOrder<std::greater, T> {};
The main drawback of this solution is the need to create a new struct every time a new variant is required, e.g.:
template <typename T>
struct SoftOrderLessThan : public SoftOrder<std::less, T> {}; // Never used after the next line.
Processor<SoftOrderLessThan> processor3;
Second Solution - A very specific bind class
template <template <typename> class Comparator>
struct BindToSoftOrder {
template <typename T>
struct type : public SoftOrder<Comparator, T> {};
};
This is slightly better in that we don't need to create the intermediate classes explicitly:
Processor<BindToSoftOrder<std::less>::type> processor3;
The downside is the requirement of a class specialised for this situation which cannot really be generalised by making SoftOrder a template parameter on BindToSoftOrder as this would make it a template<template<template>>> which is not permitted by the standard.
Third Solution - C++11 template aliases
template <typename T>
using SoftOrderLessThan = SoftOrder<std::less, T>;
Nicer than the first option in that it doesn't require the introduction of new classes, however still requires littering the code with this extra code that is only used in passing onwards to another template class:
template <typename T>
using SoftOrderLessThan = SoftOrder<std::less, T>; // Never used again
Processor<SoftOrderLessThan> processor3;
Finally, the question
Is there a generic way to bind my custom comparator to a specific comparator in the following manner?
Processor<SomeCoolMetaTemplateBind<SoftOrder, std::less>::type> processor3;
I believe if all of the template parameters were simple types I could just do something like Processor<boost::mpl::bind<SoftOrder, std::less> >, but the presence of the template type in the template parameter list prevents this from occurring.
An ideal solution would involve C++03, but am happy to hear C++11 solutions as well.
If it's not possible, I hope at least the question was interesting.
Seems like this would work:
template <
template <template <typename> class,class> class U,
template <typename> class X
>
struct SomeCoolMetaTemplateBind {
template <typename T>
struct type : public U<X,T> {
};
};

is recursive explicit template instantiation possible?

Given a template like
template<int dim> class Point { ... };
this template can be instantiated explicitly like
template class Point<0>;
template class Point<1>;
template class Point<2>;
template class Point<3>;
instead of instantiating every template individually like above, I would like to instantiate them recursively with one call like
template class RecursiveInstantiate<Point, 3>;
where RecursiveInstantiate<T, i> would instantiate T<i>, T<i-1>, ..., T<0>. Is it somehow possible to create such a class RecursiveInstantiate? If it is not possible, do you know a way to do it with the preprocessor?
In fact I am interested in generalizing this for classes with multiple template parameters likeNode<int i1,int i2,int i3> for all combination of i1,i2,i3 in {0,1,2,3}. But I hope to be able to work out this second part by myself.
Any advice, also an explanation why it is impossible what I want to achieve is appreciated.
Update: thank you for your comments so far. I see now more clearly where the problem really is. The line
template class Point<3>;
instantiates the template and exports its symbols to the object file. An instantiation of the form
template class RecursiveInstantiate<Point, 3>;
may instantiate the classes class Point<3>, class Point<2>, .... Apparently this only happens locally though. The templates are not exported to the object file. Maybe I will have to look for a solution using the preprocessor.
As I see now that I did not ask my question precisely enough in the beginning, I appreciate your answers and selected ones as correct.
Note: I am trying this on linux with g++/clang as compilers.
You could make a little Instantiator class:
template <unsigned int N> struct Instantiator
{
Point<N> p;
Instantiator<N-1> i;
};
template <> struct Instantiator<0>
{
Point<0> p;
};
Then simply add one explicit instantiation: template struct Instantiator<81>;
You can extend this idea lexicographically to any number of integral parameters.
As #Georg says, let's make it generic:
template <template <unsigned int> class T, unsigned int N> struct Instantiator
{
T<N> t;
Instantiator<T, N-1> i;
};
template <template <unsigned int> class T> struct Instantiator<T, 0>
{
T<0> t;
};
template struct Instantiator<Point, 82>;
You can do that like this:
template<int dim> struct Point {
static const int val = dim;
Point<dim - 1> p;
};
template<> struct Point<0> { ... };
That creates a template specialisation for the template parameter when it is 0 so the recursion stops there, and when you instantiate one like this:
Point<4>
It instantiates from Point<4> down to Point<0>. Then you can do
Point<4>::val
to access the value of that particular one.