Template template parameters in c++. Using instance - c++

I instance a template and want to use that instance for other template
template <typename I> class A
{
I name;
public:
A(I n){
name = n;
}
void show() const
{
cout << name << "\t";
}
};
template <template<typename I> class V> class B {
V origen;
public:
B (V o){
origen = o;
}
};
auto main() -> int
{
cout << "[code.cpp]" << endl;
A<int> a1(1);
a1.show();
B<A> b1(a1);
return 0;
}
What I see about this topic is that´s not usually use, but when use, not used in this form, and I don't understand why.
I tried using const. Not working

V origen; and void B (V o) are invalid as V is not a type.
Maybe you want
template <typename T> class B;
template <template<typename> class V, typename T>
class B<V<T>>
{
V<T> origen;
public:
B (V<T> o) : origen(o) {}
};
and then
A<int> a1(1);
B<A<int>> b1(a1);
Or maybe
template <template<typename> class V>
class B
{
V<int> origen;
public:
B (V<int> o) : origen(o) {}
};
and then
A<int> a1(1);
B<A> b1(a1);

Related

Conversions in a chain of classes

I have an object S. S is composed of layers S0, S1, S2 ... just like a stack of stackable drawers.
I want to create a chain of template classes A, B, C such that:
They represents proxies to different layers of the S object.
Even during template instantiation, C can convert to B, which can convert to A.
A, B, and C have different sets of methods.
The problem is that if I use public inheritance, then C will get the methods of A and B.
Test:
#include <iostream>
// Library
template <typename T>
class A {
public:
void a() {std::cout << "a\n"; }
int s_{0};
};
template <typename T>
class B : public A<T> {
public:
void b() {std::cout << "b\n"; }
};
template <typename T>
class C : public B<T> {
public:
void c() {std::cout << "c\n"; }
};
// User of library write a function like this
template <typename T>
void foo(A<T>& a) { a.a(); }
// The problem:
int main() {
C<int> c;
foo(c);
c.a(); // <--- how to hide this?
return 0;
}
I'm not sure if I understand what you want. But one way to do it is changing the access level of base class member in derived class. For example:
template <typename T>
class C : public B<T> {
public:
void c() { std::cout << "c\n"; }
private:
using A::a; // <-- reduce access level of base class member
};
"Just" need 0 + 1 + 2 + ... + i conversion operators for layer Si
Or one template conversion operator if their attributes are all the same.
But this still need some way to control the conversions.
#include <iostream>
template <typename T>
class A {
public:
A(int& layer) : layer_(layer) {}
void a() {std::cout << "a\n"; }
int s_{0};
private:
int& layer_;
};
template <typename T>
class B {
public:
B(int& layer) : a_(layer) { }
template <template<typename> class X, typename T2>
operator X<T2>() { return a_; }
void b() {std::cout << "b\n"; }
private:
A<T> a_;
};
template <typename T>
class C {
public:
C(int& layer) : b_(layer) {}
template <template<typename> class X, typename T2>
operator X<T2>() { return b_; }
void c() {std::cout << "c\n"; }
private:
B<T> b_;
};
template <typename T>
class D {
public:
D(int& layer) : c_(layer) {}
template <template<typename> class X, typename T2>
operator X<T2>() { return c_; }
void c() {std::cout << "c\n"; }
private:
C<T> c_;
};
template <template<typename> class X, typename T>
void foo(X<T>& a) {
A<T>(a).a();
}
int main() {
int v = 1;
D<int> d(v);
foo(d);
return 0;
}

Calling template function from inner class

I have a class like this to call a function depending on the type. I try to compile it, but have error error C2059 syntax error : 'template'
class A
{
call_1()
{
B<type> b;
b.template say(i);
}
template<class T>
struct B
{
template <typename T, typename I>
T say(I i) {
return word;
}
};
template<>
struct B<void>
{
template <typename T, typename I>
void say(I i) {
/**/
}
};
}
What am I doing wrong?
First, let's rewrite your example into something that is readable and is closer to being compilable, and we also print "1" or "2" in say() to know which function gets called:
#include <iostream>
using type = int;
class A {
void call_1() {
B<type> b;
int i = 0;
b.template say(i);
}
template<class T>
struct B
{
template <typename T, typename I>
T say(I i) {
std::cout << "1\n";
return T();
}
};
template<>
struct B<void>
{
template <typename T, typename I>
void say(I i) {
std::cout << "2\n";
}
};
};
OK, so first, you are trying to specialize B inside of A. This is not allowed, so let't move it outside of A:
using type = int;
class A {
void call_1() {
B<type> b;
int i = 0;
b.template say(i);
}
template<class T>
struct B
{
template <typename T, typename I>
T say(I i) {
std::cout << "1\n";
return T();
}
};
};
template<>
struct A::B<void>
{
template <typename T, typename I>
void say(I i) {
std::cout << "2\n";
}
};
Next up, you are using the same template parameter (T) in both B and say(). You don't need to repeat T, so let's delete it:
using type = int;
class A {
void call_1() {
B<type> b;
int i = 0;
b.template say(i);
}
template<class T>
struct B
{
template <typename I>
T say(I i) {
std::cout << "1\n";
return T();
}
};
};
template<>
struct A::B<void>
{
template <typename I>
void say(I i) {
std::cout << "2\n";
}
};
Finally, call_1() cannot be defined before the specialization of A::B, so we need to move it outside too:
using type = int;
class A {
void call_1();
template<class T>
struct B
{
template <typename I>
T say(I i) {
std::cout << "1\n";
return T();
}
};
};
template<>
struct A::B<void>
{
template <typename I>
void say(I i) {
std::cout << "2\n";
}
};
void A::call_1() {
B<type> b;
int i = 0;
b.template say(i);
}
This should now compile and do what you want. Calling call_1() will print 1. If you change the type from int to void:
using type = void;
it will print 2.

templated template specialization?

Say I want to write a wrapper for 2 or more classes that do the same things with different implementations and their interfaces have different function names. Depending on the context I would choose the one or the other, but I want to be able to easily switch them out. SO I write a wrapper with template specialization. Ok good. Now however I encountered a problem. My 2 classes are template classes...
If they were normal classes I could write code like this:
class A1
{
public:
int f()
{
return 1;
}
};
class A2
{
public:
int g()
{
return 1;
}
};
namespace detail
{
template <class T> int h(T& t) // general case
{
std::cout << "general" << "\n";
return t.h();
}
template <> int h<A1>(A1& a1) // case for A1
{
std::cout << "A1" << "\n";
return a1.f();
}
template <> int h<A2>(A2& a2) // case for A2
{
std::cout << "A2" << "\n";
return a2.g();
}
}
template <class T>
class Wrapper
{
public:
Wrapper(T& t) : t(t) {}
int operator()()
{
return detail::h<T>(t);
}
T& t;
};
However, how would I need to modify that code to make it run for tempalted versions of A1 and A2? The best I came up with was this (does not compile):
template <class T>
class A1
{
public:
int f()
{
return 1;
}
};
template <class T>
class A2
{
public:
int g()
{
return 1;
}
};
namespace detail
{
template <class T, class U> int h(T<U>& t) // general case
{
return t.h();
}
template <> int h<A1<U>>(A1<U>& a1) // case for A1
{
return a1.f();
}
template <> int h<A2<U>>(A2<U>& a1) // case for A2
{
return a1.f();
}
}
template <class T, class U>
class Wrapper
{
public:
Wrapper(T<U>& t) : t(t) {}
int operator()()
{
return detail::h<T,U>(t);
}
T<U>& t;
};
So, I somehow need to template the template specializations, which sounds like a contradiction.
edit
Ok.. trying to make the overload solution work, but I don't really get it...
template <template <typename> class T, class U>
class Wrapper
{
public:
Wrapper(T<U>& t) : t(t) {}
template <template <typename> class T, typename U>
int h(T<U>& t) // general case
{
return t.h();
}
template <typename U>
int h(A1<U>& a1) // case for A1
{
return a1.f();
}
template <typename U>
int h(A2<U>& a2) // case for A2
{
return a2.g();
}
T<U>& t;
};
Prefer overload to template specialisation:
template <template <typename> class T, typename U>
int h(T<U>& t) // general case
{
return t.h();
}
template <typename T>
int h(A1<T>& a1) // case for A1
{
return a1.f();
}
template <typename T>
int h(A2<T>& a2) // case for A2
{
return a2.g();
}

Can we add a friend class based on template parameter?

I wonder whether the below tricky situation is possible:
Suppose I have a template class template <typename DTYPE> class A{};, where DTYPE is supposed to be one of uint8_t, uint16_t, etc. I want to add a friend class to A, but this friend class differs for each DTYPE alternative. Further, suppose the friend classes for different DTYPE values are not instantiations of another template class, but independent classes.
Is there a way to do it?
You can add template "proxy" class FriendOfA and specialize it for whatever type you need:
// actual friends
class FriendUint8 {};
class FriendUint16 {};
template<typename T> struct FriendOfA;
template<>
struct FriendOfA<uint8_t> {
typedef FriendUint8 type;
};
template<>
struct FriendOfA<uint16_t> {
typedef FriendUint16 type;
};
// optional helper
template <typename T>
using FriendOfA_t = typename FriendOfA<T>::type;
template<class T>
class A {
friend typename FriendOfA<T>::type;
// or simply
friend FriendOfA_t<T>;
};
I believe you're looking for something like that:
#include <iostream>
struct BaseFriend
{
template <typename T>
void boo(const T& t) { t.foo(); }
};
struct BaseFriendProxy
{
template <typename T>
void boo(const T& t) { std::cout << "Proxy: "; t.foo(); }
};
template <typename TType>
struct Friend ;
template <>
struct Friend<int> {
using T = BaseFriend;
};
template <>
struct Friend<char> {
using T = BaseFriendProxy;
};
template <typename DTYPE>
class A
{
private:
friend typename Friend<DTYPE>::T;
void foo() const
{ std::cout << "A::foo()" << std::endl; }
};
int main()
{
A<int> a;
BaseFriend bf1;
bf1.boo(a);
A<char> b;
BaseFriendProxy bf2;
bf2.boo(b);
return 0;
}
But this will work only with C++11: you can't combine friend class ... with typename X::Y in C++03
Sure you can, specialize your template and add whatever friend class you want:
#include <iostream>
using namespace std;
class base {
public:
virtual int getValue() = 0;
};
class friend1 {
public:
base* ptr;
int getValue() {
return ptr->getValue();
}
};
class friend2 {
public:
base* ptr;
int getValue() {
return ptr->getValue();
}
};
template <typename DTYPE> class A : public base{
public:
A() { data = 42; }
// No friends
private:
int data;
int getValue() {
return data;
}
};
template <> class A<char> : public base{
public:
A() { data = 44; }
friend class friend1;
private:
int data;
int getValue() {
return data;
}
};
template <> class A<bool> : public base{
public:
A() { data = 45; }
friend class friend2;
private:
int data;
int getValue() {
return data;
}
};
int main()
{
A<char> obj1;
friend1 friend_of_obj1;
friend_of_obj1.ptr = &obj1;
cout << friend_of_obj1.getValue() << endl;
A<bool> obj2;
friend2 friend_of_obj2;
friend_of_obj2.ptr = &obj2;
cout << friend_of_obj2.getValue();
}
http://ideone.com/hM9x0y
Yes, a friend can be based on a template. Such as;
template <typename DTYPE>
struct Friend;
template <class DTYPE>
class A {
friend struct Friend<DTYPE>;
};
For each type DTYPE for the class A you wish to support (different by implementation) you can specialise Friend, such as;
template<>
struct Friend<uint8_t> {
// ...
};
A basic working sample:
#include <cstdint>
using namespace std;
template<typename T>
struct Friend;
template <class T>
class A {
friend struct Friend<T>;
int i = 0;
};
template<>
struct Friend<uint8_t> {
void method() {
A<uint8_t> a;
a.i = 8;
}
};
template<>
struct Friend<uint16_t> {
void method() {
A<uint16_t> a;
//A<uint8_t> b; // fails to compile
a.i = 16;
}
};
int main()
{
A<uint8_t> a;
Friend<uint8_t> f;
f.method();
}

Overload template member function over any container of A or B

I have 2 existing classes A and B. I want to implement a third struct C such that C implement operator() which would take any container of A or using a different implementation any container of B. Is it possible to do a such thing using template specialization ?
enable_if is very useful in lots of scenarios, but in cases like these I'm usually more inclined to use tag dispatching. To my eye, the code looks cleaner, behaves more predictably, and if you try to use it wrongly, the error messages make slightly more sense.
struct C
{
template <class T>
void operator()(const T& container) const
{
operator()(container, Tag<typename T::value_type>());
}
private:
template <class V> struct Tag {};
template <class T>
void operator()(const T& container, Tag<A>) const
{
std::cout << "A\n";
}
template <class T>
void operator()(const T& container, Tag<B>) const
{
std::cout << "B\n";
}
};
int main()
{
std::vector<A> a;
std::list<B> b;
C c;
c(a);
c(b);
}
You can do this using template template parameters.
Assuming your containers have two parameters (content type and allocator):
class A {};
class B {};
struct C {
template <
template <
typename,
typename
>
class V
>
void operator()(const V<A, std::allocator<A> >& /* ca */) const {
}
template <
template <
typename,
typename
>
class V
>
void operator()(const V<B, std::allocator<B> >& /* cb */) const {
}
};
Which would then allow you to do the following:
int main() {
std::vector<A> va;
std::vector<B> vb;
std::list<A> la;
std::list<B> lb;
C c;
c(va);
c(la);
c(vb);
c(lb);
}
Overloads using std::enable_if and std::is_same seem to work. Not very pretty though. Note that these are c++11 features.
struct A {};
struct B {};
struct C {
template<class Cont>
void operator()(const Cont& c, typename std::enable_if<std::is_same<typename Cont::value_type, A>::value>::type * = NULL) const {
std::cout << "a";
}
template<class Cont>
void operator()(const Cont& c, typename std::enable_if<std::is_same<typename Cont::value_type, B>::value>::type * = NULL) const {
std::cout << "b";
}
};
int main() {
std::vector<A> a;
std::list<B> b;
C c;
c(a);
c(b);
}
Yes it should be possible. Arkanosis has a good solution but here is if you want to use template specialization:
class A {};
class B {};
template <template <typename , typename > class Z, class Elem> struct C
{
void operator()(const Z<Elem, std::allocator<Elem>>& v) { std::cout << "General implement" << std::endl; }
};
template <template <typename, typename > class Z> struct C<Z,B>
{
void operator()(const Z<B, std::allocator<B>>& v) { std::cout << "Specialized implement" << std::endl; }
};
Then some client code:
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv)
{
std::vector<A> as;
std::list<B> bs;
C<vector,A> ca;
ca(as);
C<list,B> cb;
cb(bs);
}
If you want to handle different container types from the same C-style class you can derive from templates of the C struct e.g. class MultiContainerHandlerForA : public C<vector,A>, public C<list,A>.