Calling function with two template parameters - c++

I am having a problem calling a template function with two template arguments.
I have a class and the class accepts objects of two different types. I don't know the types yet, so I left them as template parameters. I then store the objects in wrapper classes. In the end I want to be able to call a templated function with two template arguments, that takes my two objects. But I am perplexed at how to do this.
Here is a stripped down version of the code to explain my problem.
template<typename A, typename B>
void someTemplateFunction(A a, B b);
class Problem
{
private:
class WrapperA
{
public:
virtual void doSomething() = 0;
};
template<typename A>
class ConcreteWrapperA : public wrapperA
{
private:
A a;
public:
ConcreteWrapperB(A b_) : a(a_) {}
virtual void doSomething();
};
class WrapperB
{
public:
virtual void doSomething() = 0;
};
template<typename B>
class ConcreteWrapperB : public wrapperB
{
private:
B b;
public:
ConcreteWrapperB(B b_) : b(b_) {}
virtual void doSomething();
};
WrapperA *a;
WrapperB *b;
public:
template<typename A>
void setA(A a)
{
a = new ConcreteWrapperA<A>(a);
}
template<typename B>
void setB(B b)
{
a = new ConcreteWrapperB<B>(b);
}
void call_someTemplateFunction(); // ??????? How do i do this?
};

The problem is that you've type-erased both types A and B separately, so there's nowhere in the translation of your code that both types A and B are known.
If you can write a single function template<typename A, typename B> void set(A, B) then you could capture the pair type <A, B> at that point.
Alternatively, would it be possible for someTemplateFunction to operate without knowing both types A and B at the same time?
This is an issue fundamental to the design of C++ as a single-pass, separate compilation language.
Suppose that your program has three compilation units; A.cpp calls setA with a range of types T[A], B.cpp calls setB with another range of types T[B], and C.cpp owns the Problem object and wants to call someTemplateFunction. Then there's no time during compilation when the compiler knows both the range of types in A.cpp and the range of types in B.cpp, so it can't instantiate someTemplateFunction with the appropriate cross-product T[A] x T[B].

Related

C++ member access from a derived class of a templated type

Long story short, what I want here is to declare a templated type in a base class and be able to access that type A<T> such that the base class B contains it and the derived class C is able to access it as C::A<T>. I did try declaring an int inside of class B and that can be accessed from the derived C class as C::int, here's the error!
||In constructor ‘D::D()’:|
|74|error: no match for ‘operator=’ (operand types are ‘A<C*>’ and ‘A<B*>’)|
|4|note: candidate: A<C*>& A<C*>::operator=(const A<C*>&)|
|4|note: no known conversion for argument 1 from ‘A<B*>’ to ‘const A<C*>&’|
And this is the code that does compile ( comment A<B*> i; and uncomment A<C*> i; to get the error).
#include <iostream>
//class with a template parameter
template <class a>
class A
{
private:
int somevalue;
public:
A(){}
~A(){}
void print()
{
std::cout<<somevalue<<std::endl;
}
};
//1. could forward declare
class C;
class B
{
protected:
A<B*> i;
//2. and then use
//A<C*> i;
public:
B(){}
~B(){}
A<B*> get()
{
return i;
}
/*
//3. use this return instead
A<C*> get()
{
return i;
}
*/
};
//specialization of B that uses B's methods variables
class C : public B
{
protected:
public:
C(){}
virtual ~C(){}
void method()
{
B::i.print();
}
};
//class D that inherits the specialization of C
class D : public C
{
private:
A<B*> i;//works
//4. but I want the inherited type to work like
//A<C*> i;// so that the type C* is interpreted as B*
public:
D()
{
this->i = C::i;
}
~D(){}
};
///////////////////////////////////////////////////////////////////////
int main()
{
D* d = new D();
delete d;
return 0;
}
But okay what if we tried this std::list<template parameter> LIST and then plug that in? That's the problem A<T> is std::list.
As far as I understand your issue now you seem to have a std::list<Base *> (renamed B to Base for clarity) and want to fill an std::list<Concrete*> (renamed C to Concrete, it's derived from Base) with it.
For that you need to iterate over the Base* pointers, checking for each whether it can be downcast to a Concrete* and if so adding it to the std::list<Concrete*>. You need to think about what to do if the downcast fails, too.
For all of this to work your Base needs to be a polymorphic base class, that is it must contain a virtual member function (don't forget to make the destructor virtual). Also note that this sounds like a catastrophe waiting to happen in terms of managing ownership of those pointers.
template<typename Base, typename Concrete>
std::list<Concrete*> downcast_list (std::list<Base*> const & bases) {
std::list<Concrete*> result;
for (auto const base_ptr : bases) {
Concrete * concrete_ptr = dynamic_cast<Concrete*>(base_ptr);
if (concrete_ptr != nullptr) {
result.push_back(concrete_ptr);
} else {
// Error or ignore?
}
}
return result;
}
Note: a more idiomatic version of this would use iterators.
I found the pattern to my problem, it's actually really simple and it serves as the base for encapsulating a class type a (which is a template parameter to be passed around, try looking at my question as a reference to class a). The pattern is shown below, it's generally what I wanted. I found it on this webpage Using Inheritance Between Templates chapter 7.5 from the book entitled OBJECT-ORIENTED
SOFTWARE DESIGN
and CONSTRUCTION
with C++ by Dennis Kafura. I'll copy it below the edited code for the sake of future reference in case anyone else needs it.
template <class a>
class B
{
private:
public:
B();
~B();
};
template <class a>
class C : public B<a>
{
public:
C();
~C();
};
This is the code it was adapted from.
template <class QueueItem> class Queue
{
private:
QueueItem buffer[100];
int head, tail, count;
public:
Queue();
void Insert(QueueItem item);
QueueItem Remove();
~Queue();
};
template <class QueueItem> class InspectableQueue : public Queue<QueueItem>
{
public:
InspectableQueue();
QueueItem Inspect(); // return without removing the first element
~InspectableQueue();
};
Try changing this:
#include <iostream>
//class with a template parameter
template <class a>
class A {
private:
int somevalue;
public:
A(){}
~A(){}
void print() {
std::cout<<somevalue<<std::endl;
}
};
//1. could forward declare
class C;
class B {
protected:
A<B*> i;
//2. and then use
//A<C*> i;
public:
B(){}
~B(){}
A<B*> get() {
return i;
}
/*/3. use this return instead
A<C*> get() {
return i;
} */
};
//specialization of B that uses B's methods variables
class C : public B {
protected:
public:
C(){}
virtual ~C(){}
void method() {
B::i.print();
}
};
//class D that inherits the specialization of C
class D : public C {
private:
A<B*> i;//works
//4. but I want the inherited type to work like
//A<C*> i;// so that the type C* is interpreted as B*
public:
D() {
this->i = C::i;
}
~D(){}
};
int main() {
D* d = new D();
delete d;
return 0;
}
To Something Like This:
#include <iostream>
//class with a template parameter
template <typename T>
class Foo {
private:
T value_;
public:
Foo(){} // Default
Foo( T value ) : value_(value) {}
~Foo(){}
void print() {
std::cout<< value_ << std::endl;
}
};
class Derived;
class Base {
protected:
Foo<Base*> foo_;
Base(){} // Default;
virtual ~Base(){}
// Overload This Function
template<typename T = Base>
/*virtual*/ Foo<T*> get();
/*virtual*/ Foo<Base*> get() { return this->foo_; }
/*virtual*/ Foo<Derived*> get();
};
class Derived : Base {
public:
Derived() {}
virtual ~Derived() {}
void func() {
Base::foo_.print();
}
void Foo<Derived*> get() override { return this->foo_; }
};
And this is as about as far as I could get trying to answering your question...
There are objects that you are not using in your code
There are methods that aren't being called.
It is kind of hard to understand the direction/indirection
of what you mean to do with the inheritance tree.
You are inheriting from a base class without a virtual destructor
And probably a few other things that I can not think of off the top of my head right now.
I'd be more than willing to try and help you out; but this is as far as I can go with what you currently are showing.
EDIT -- I made changes to the base & derived classes and removed the virtual keyword to the overloaded function template declarations - definitions belonging to those classes.

Class reference another two classes

I have two classes with some methods with same name.
Can I create third class that accept reference from ony of the other two and in the constructor to set obj variable to A or B type?
class A
{
public:
A();
void f();
};
class B
{
public:
B();
void f();
};
class C
{
public:
C(B&);
C(A&);
??? obj;
};
Maybe you want a template class:
template <typename T>
class C
{
T& obj;
public:
explicit C(T& t) : obj(t) {}
void f() { obj.f(); }
};
And then:
A a;
B b;
C<A> c1(a);
C<B> c2(b);
c1.f();
c2.f();
C++ is a very flexible language and as such provides multiple options for what you are asking for. Each with their own pros and cons.
The first route that comes to mind is to use polymorphism.
You have two routes to choose from: static or dynamic polymorphism.
The Static Polymorphic Route
To use static polymorphism (also known as compile-time polymorphism) you should make C a template class:
template <typename T> class C
{
public:
C(T&);
T& obj;
}
The Dynamic Polymorphic Route
To use dynamic (also known as run-time polymorphism) you should provide an interface:
class Fer
{
public:
virtual ~Fer() {}
virtual void f() = 0;
}
Which A and B would implement:
class A : public Fer
{
public:
A();
void f() overide;
};
class B : public Fer
{
public:
B();
void f() overide;
};
C would then be like this:
class C
{
public:
C(Fer&);
Fer& obj;
}
The Variant Route
There are various libraries that provide classes that can safely hold arbitrary types.
Some examples of these are:
Boost.Any
Boost.Variant
QVariant from Qt
When using such classes you generally need some means of converting back to the actual type before operating on it.
You can have a base class that defines the required interface.
class Base
{
public:
Base();
virtual void f();
};
And you can have derived classes that implement the interface.
class A : public Base
{
public:
A();
virtual void f();
};
class B : public Base
{
public:
B();
virtual void f();
};
The class C then refers to the Base class and can actually accept objects of A or B type.
class C
{
private:
Base& base;
public:
C(Base& b) : base(b) {}
};
It can be easily used then.
int main()
{
B b;
C c(b);
return 0;
}

How to define generic template signature

I have 2 classes where the functions are copy and paste. The only difference between these classes is class each inherit. These super classes have same call signatures, but the constructors are different.
I was trying to do something like:
template<class BaseClass>
class IGenericPart : public BaseClass {
public:
int commonCall1() { return 10; }
int commonCall2() { return 44; }
};
class A : public IGenericPart<RealBaseClass1> {
public:
A(int x) : IGenericPart<RealBaseClass1> (x, b) {}
};
class B : public IGenericPart<RealBaseClass2> {
public:
B(string z) : IGenericPart<RealBaseClass2> (z, anothertype2, anothertype2) {}
};
But I don't know how to make the IGenericPart class having a forward constructor based on the generic BaseClass.
So basically, I need that the IGenericPart<RealBaseClass1> and IGenericPart<RealBaseClass2> having different constructors.
You're just missing a using declaration.
When class IGenericPart<> inherits from BaseClass it does not automatically inherit the constructors from BaseClass. To inherit constructors, you must do so explicitly with a using declaration, like so:
template<class BaseClass>
class IGenericPart : public BaseClass {
public:
using BaseClass::BaseClass;
// etc ...
};
This then creates constructors for IGenericPart<BaseClass> that are in correspondence with and which forward to each constructor of BaseClass, so the initialization lists of A and B will work as expected.
Note that constructor inheritence is a C++11 feature, so you must be compiling in C++11 mode with a compiler that supports C++11 for this to work.
If you can't use c++11 for some reasone you can get it to work by adding the following, clunky bits, to IGenericPart:
template<class BaseClass>
class IGenericPart: public BaseClass {
public:
template <typename A>
IGenericPart(A a) : BaseClass(a) {}
template <typename A, typename B>
IGenericPart(A a, B b) : BaseClass(a, b) {}
template <typename A, typename B, typename C>
IGenericPart(A a, B b, C c) : BaseClass(a, b, c) {}
}

C++: Class - Overloaded Constructors - single variable?

I am not sure how to ask this, but hopefully someone will understand. Lets say I have 3 different classes. Class A, Class B and Class C. Class C should take either Class A or Class B as a parameter in the constructor and store it in a private variable.
This is easy with overloaded constructors. My question is how can Class C automagically use the correct class depending on what constructor was used? (Note these 2 classes are similar, but come from different libraries and thus no shared base class). Is this possible with templates? I do not have a lot of experience with templates.
You can do it quite easy with templates:
class A;
class B;
template<class AorB>
class C
{
public:
C(AorB aorb)
: aorb_(aorb)
{ }
private:
AorB aorb_;
};
What this does is that inside the class C the identifier AorB can be used as any other class, in fact it doesn't even have to be an instance of A or B but can be any class.
Can be used like this:
A myA;
B myB;
C<A> myCWithA(myA);
C<B> myCWithB(myB);
There is however one thing you have to remember when creating classes using templates: The specification and implementation can no longer be split into separate header and source files. All of the code have to be available in the header file.
The syntax of the member functions are also a little different.
Example:
template<class T>
class C
{
public:
...
void someFunction();
};
template<class T>
C<T>::someFunction()
{
...
}
Yes, this is possible with templates:
#include <iostream>
template<class T>
class C {
public:
C(T const& ref) : ref(ref) {}
void doStuff() const {
ref.doStuff();
}
private:
T ref;
};
class A {
public:
void doStuff() const {
std::cout << "A::doStuff" << std::endl;
}
};
class B {
public:
void doStuff() const {
std::cout << "B::doStuff" << std::endl;
}
};
int main() {
C<A> foo((A()));
foo.doStuff();
C<B> bar((B()));
bar.doStuff();
}

Moving Template Method to Derivative Breaks Compilation

Given the following code:
template<typename T>
class A
{
public:
T t;
};
class B
{
public:
void foo(int i) {}
template<typename T>
void foo(A<T>& a) {}
};
int main()
{
A<int> a;
B b;
b.foo(a );
b.foo(a.t);
}
This compiles and works fine; the correct overloaded versions of B::foo() are chosen and called for a and a.t.
Now I introduce a new class C which derives from B and move the template version of ::foo() out of B and into C:
template<typename T>
class A
{
public:
T t;
};
class B
{
public:
void foo(int i) {}
};
class C: public B
{
public:
template<typename T>
void foo(A<T>& a) {}
};
int main()
{
A<int> a;
C c;
c.foo(a ); // Fine
c.foo(a.t); // Error
}
And now the code won't compile anymore. Visual Studio 2005 is stating:
error C2784: 'void C::foo(A<T> &)' : could not deduce template argument for 'A<T> &' from 'int'
In fact, calling C::foo() with any int value results in this error. It almost seems like the method overload for int is being hidden by the template overload.
Why is this happening? Is it some issue with Visual Studio 2005's compiler? Unfortunately, I cannot test it on any other compiler right now.
Any information is appreciated.
It almost seems like the method overload for int is being hidden by the template overload.
Exactly! You need to add a using declaration to class C:
class C: public B
{
public:
using B::foo;
template<typename T>
void foo(A<T>& a) {}
};
When you declare a member function in a derived class, all member functions in the base class with the same name are hidden. See §3.3.10/3 of ISO/IEC 14882:2011:
The declaration of a member in a derived class (Clause 10) hides the declaration of a member of a base class of the same name; see 10.2.
It's hiding, no overloading. Use
class C: public B
{
public:
using B::foo;
template<typename T>
void foo(A<T>& a) {}
};
Correct, the base function is hidden. That's actually the proper term for it. Add using B::foo; to the class definition of C to unhide it.