Here is the test code:
class A{};
class B : public A{};
void Test(const std::vector<A>& a)
{
}
int main()
{
std::vector<A> a;
std::vector<B> b;
Test(a);
Test(b);//Compiler Error
return 0;
}
Since std::vector<A> and std::vector<B> is different type, we can't make conversion from one to another.
An optional way can be:
class A{};
class B : public A{};
void Test(const std::vector<A>& a)
{
}
int main()
{
std::vector<A> a;
std::vector<B> b;
Test(a);
Test(std::vector<A>(b.begin(), b.end()));
return 0;
}
It acts, but it takes extra copying from B to A. If A or B is a large object, it can be very slow. A better choice is:
class A{};
class B : public A{};
void Test(const std::vector<A>& a)
{
}
int main()
{
std::vector<A> a;
std::vector<B> b;
Test(a);
Test(std::vector<A>(std::make_move_iterator(b.begin()), std::make_move_iterator(b.end())));
return 0;
}
Since it just move the iter instead of the whole B class, it take a better performance. But there are some extra cost - If b is a very large vector with huge number of items, iteration also can slow down my code.
So I wonder if there is a way to directly convert std::vector<B> to std::vector<A> without any extra cost?
One approach is to make the vector a template parameter of the Test function. The calling code will remain the same, there is no copying required.
#include <vector>
#include <iostream>
class A{
public:
virtual void print() const {
std::cout << "A" << std::endl;
}
};
class B : public A{
virtual void print() const {
std::cout << "B" << std::endl;
}
};
template <typename ContainerA>
void Test(const ContainerA& aas)
{
for(const A& a:aas) {
a.print();
}
}
int main()
{
std::vector<A> a;
a.push_back(A());
std::vector<B> b;
b.push_back(B());
Test(a);
Test(b);
return 0;
}
I don't know if it's useful for you but the best I can think is pass std::vector<Derived> to the function and iterate the element as Base elements.
Something like
template <typename D>
void Test (std::vector<D> const & v)
{
for ( A const & elem : v )
{
// use elem
}
}
Using SFINAE you can enable Test() only for vectors of Base or derived from Base elements as follows
template <typename D>
typename std::enable_if<std::is_base_of<A, D>::value>::type
Test (std::vector<D> const & v)
{
for ( A const & elem : v )
{
// use elem
}
}
So you have
int main ()
{
std::vector<A> a;
std::vector<B> b;
std::vector<int> c;
Test(a); // compile
Test(b); // compile
// Test(c); // compiler error
}
Related
Having this code:
#include <iostream>
#include <vector>
#include <string>
class A{
public:
A(std::string const& name) : name(name) {}
std::string const& getName() const {
return name;
}
private:
std::string name;
};
class B{
public:
void add(A const& a){
//vec.emplace_back(a) does not help either
vec.push_back(a);
}
std::vector<A> const& getVec() const {
return vec;
}
private:
std::vector<A> vec;
};
class C{
public:
C(B const& b, A const& a) : a(a), b(b) {}
A const& getA() const {
return a;
}
B& getB() {
return b;
}
private:
A a;
B b;
};
class D{
public:
C& add(C& c){
A const& a = c.getA();
B& b = c.getB();
b.add(a);
vec.push_back(c);
return c;
}
private:
std::vector<C> vec;
};
int main(){
A a1{"first"};
A a2{"second"};
B b;
C c1{b, a1};
C c2{b, a2};
D d;
d.add(c1);
d.add(c2);
for(A const& a : b.getVec()){
std::cout << a.getName() << ' ';
}
}
There is no output from the program, but
I would expect output:
first second
Where the B class is holding the vector, and D is trying to modify it (c.getB().add()) via class C. It's just simple example (encountered in a school project), but I wanted to know by this example the flow of changes between classes.
C's members are not reference types. Both c1 and c2 are storing copies of the constructor arguments.
So
d.add(c1);
d.add(c2);
doesn't affect b in main, only the members of c1 and c2, which are copies of the original b, a1 and a2 in main.
Consider this pseudo-snippet:
class SomeClass
{
public:
SomeClass()
{
if(true)
{
fooCall = [](auto a){ cout << a.sayHello(); };
}
else
{
fooCall = [](auto b){ cout << b.sayHello(); };
}
}
private:
template<typename T>
std::function<void(T)> fooCall;
};
What I want is a class member fooCall which stores a generic lambda, which in turn is assigned in the constructor.
The compiler complains that fooCall cannot be a templated data member.
Is there any simple solution on how i can store generic lambdas in a class?
There is no way you'll be able to choose between two generic lambdas at run-time, as you don't have a concrete signature to type-erase.
If you can make the decision at compile-time, you can templatize the class itself:
template <typename F>
class SomeClass
{
private:
F fooCall;
public:
SomeClass(F&& f) : fooCall{std::move(f)} { }
};
You can then create an helper function to deduce F:
auto makeSomeClassImpl(std::true_type)
{
auto l = [](auto a){ cout << a.sayHello(); };
return SomeClass<decltype(l)>{std::move(l)};
}
auto makeSomeClassImpl(std::false_type)
{
auto l = [](auto b){ cout << b.sayHello(); };
return SomeClass<decltype(l)>{std::move(l)};
}
template <bool B>
auto makeSomeClass()
{
return makeSomeClassImpl(std::bool_constant<B>{});
}
I was not able to store std::function<> as a generic lambda in the class directly as a member. What I was able to do was to specifically use one within the class's constructor. I'm not 100% sure if this is what the OP was trying to achieve but this is what I was able to compile, build & run with what I'm suspecting the OP was aiming for by the code they provided.
template<class>
class test {
public: // While testing I changed this to public access...
// Could not get object below to compile, build & run
/*template<class U = T>
static std::function<void(U)> fooCall;*/
public:
test();
};
template<class T>
test<T>::test() {
// This would not compile, build & run
// fooCall<T> = []( T t ) { std::cout << t.sayHello(); };
// Removed the variable within the class as a member and moved it here
// to local scope of the class's constructor
std::function<void(T)> fooCall = []( auto a ) { std::cout << a.sayHello(); };
T t; // created an instance of <Type T>
fooCall(t); // passed t into fooCall's constructor to invoke the call.
}
struct A {
std::string sayHello() { return "A say's Hello!\n"; }
};
struct B {
std::string sayHello() { return "B say's Hello!\n"; }
};
int main() {
// could not instantiate an object of SomeClass<T> with a member of
// a std::function<> type that is stored by a type of a generic lambda.
/*SomeClass<A> someA;
SomeClass<B> someB;
someA.foo();
someB.foo();*/
// Simply just used the object's constructors to invoke the locally stored lambda within the class's constructor.
test<A> a;
test<B> b;
std::cout << "\nPress any key & enter to quit." << std::endl;
char c;
std::cin >> c;
return 0;
}
With the appropriate headers the above as is should compile, build & run giving the output below (At least in MSVS 2017 on Windows 7 64bit did); I left comments where I ran into errors and tried multiple different techniques to achieve a working example, errors occurred as others suggested and I found even more while working with the above code. What I was able to compile, build and run came down to this simple bit of code here without the comments. I also added another simple class to show it will work with any type:
template<class>
class test {
public:
test();
};
template<class T>
test<T>::test() {
std::function<void( T )> fooCall = []( auto a ) { std::cout << a.sayHello(); };
T t;
fooCall( t );
}
struct A {
std::string sayHello() { return "A say's Hello!\n"; }
};
struct B {
std::string sayHello() { return "B say's Hello!\n"; }
};
struct C {
int sayHello() { return 100; }
};
int main() {
test<A> testA;
test<B> testB;
test<C> testC;
std::cout << "\nPress any key & enter to quit." << std::endl;
char c;
std::cin >> c;
return 0;
}
Output:
A say's Hello!
B say's Hello!
100
Press any key & enter to quit
I don't know if this will help the OP directly or indirectly or not but if it does or even if it doesn't it is still something that they may come back to and build off of.
you can simply use a template class or...
If you can get away with using c++17, you could make fooCall's type std::function<void(const std::any&)> and make a small wrapper for executing it.
method 1 : simply use a template class (C++14).
method 2 : seems to mimic the pseudo code exactly as the OP intended (C++17).
method 3 : is a bit simpler and easier to use than method 2 (C++17).
method 4 : allows us to change the value of fooCall (C++17).
required headers and test structures for the demo :
#include <any> //not required for method 1
#include <string>
#include <utility>
#include <iostream>
#include <functional>
struct typeA {
constexpr const char * sayHello() const { return "Hello from A\n"; }
};
struct typeB {
const std::string sayHello() const { return std::string(std::move("Hello from B\n")); }
};
method 1 :
template <typename T>
class C {
const std::function<void(const T&)> fooCall;
public:
C(): fooCall(std::move([](const T &a) { std::cout << a.sayHello(); })){}
void execFooCall(const T &arg) {
fooCall(arg);
}
};
int main (void) {
typeA A;
typeB B;
C<typeA> c1;
C<typeB> c2;
c1.execFooCall(A);
c2.execFooCall(B);
return 0;
}
method 2 :
bool is_true = true;
class C {
std::function<void(const std::any&)> fooCall;
public:
C() {
if (is_true)
fooCall = [](const std::any &a) { std::cout << std::any_cast<typeA>(a).sayHello(); };
else
fooCall = [](const std::any &a) { std::cout << std::any_cast<typeB>(a).sayHello(); };
}
template <typename T>
void execFooCall(const T &arg) {
fooCall(std::make_any<const T&>(arg));
}
};
int main (void) {
typeA A;
typeB B;
C c1;
is_true = false;
C c2;
c1.execFooCall(A);
c2.execFooCall(B);
return 0;
}
method 3 :
/*Note that this very closely resembles method 1. However, we're going to
build off of this method for method 4 using std::any*/
template <typename T>
class C {
const std::function<void(const std::any&)> fooCall;
public:
C() : fooCall(std::move([](const std::any &a) { std::cout << std::any_cast<T>(a).sayHello(); })) {}
void execFooCall(const T &arg) {
fooCall(std::make_any<const T&>(arg));
}
};
int main (void) {
typeA A;
typeB B;
C<typeA> c1;
C<typeB> c2;
c1.execFooCall(A);
c2.execFooCall(B);
return 0;
}
method 4 :
/*by setting fooCall outside of the constructor we can make C a regular class
instead of a templated one, this also complies with the rule of zero.
Now, we can change the value of fooCall whenever we want.
This will also allow us to do things like create a container that stores
a vector or map of functions that each take different parameter types*/
class C {
std::function<void(const std::any&)> fooCall; //could easily be replaced by a vector or map
public:
/*could easily adapt this to take a function as a parameter so we can change
the entire body of the function*/
template<typename T>
void setFooCall() {
fooCall = [](const std::any &a) { std::cout << std::any_cast<T>(a).sayHello(); };
}
template <typename T>
void execFooCall(const T &arg) {
fooCall(std::make_any<const T&>(arg));
}
};
int main (void) {
typeA A;
typeB B;
C c;
c.setFooCall<typeA>;
c.execFooCall(A);
c.setFooCall<typeB>;
c.execFooCall(B);
return 0;
}
Output from Any method
Hello from A
Hello from B
I want to create typesafe structures that are basically identical but have different types so that they require different function signatures.
struct A {
Time t;
void doStuff(const A&);
A getStuff();
};
struct B {
Time t;
void doStuff(const B&);
B getStuff();
};
If I sue a template for the class
template<class T>
struct X {
Time t;
void doStuff(const X&);
X getStuff();
};
how can I make functions typesafe and define function signatures differently for a struct X of type A and a struct X of type B?
Try adding some unused template parameters.
template <int>
struct X{
Time t;
void doStuff(const X&); // You missed return type
X getStuff();
}; // You missed a semicolon
// Great thanks to "aschepler"
And now you can (C++11 syntax)
using A = X<1>;
using B = X<2>;
// typedef X<1> A;
// typedef X<2> B;
The following code will fail, which is what you want:
A a; B b;
a.doStuff(b); // Fail
a = b.getStuff(); // Fail
You could use a combination of Inheritance and template for that.
Following your line of thought, a possible code is:
template <class T> struct X {
int time = 10;
virtual void doStuff(const T&) = 0;
virtual T getStuff() = 0;
};
struct A : X<A>
{
void doStuff(const A& a) {
cout << "Do A stuff" << endl;
}
A getStuff() {
return *this;
}
};
struct B : X<B>
{
void doStuff(const B& value) {
cout << "Do B stuff" << endl;
}
B getStuff() {
return *this;
}
};
Then, testing we have:
int main()
{
A a; B b;
a.doStuff(a); // Ok
b.doStuff(b); // Ok
a.doStuff(b); // fail
b.doStuff(a); // fail
b = b.getStuff(); // Ok
b = a.getStuff(); // fail
return 0;
}
With a normal class. For example:
class A {
public:
int a;
std::string b;
A() {}
~A() {}
}
We can do:
A x;
x.a = 1;
x.b = "hello";
But now I don't want to do like above. I want to access n_index-th attribute of object. For example pseudo like x.get<2>() (or x.set<2>(...)) like x.b.
How can do that? Have any template for that.
Beside if I want code like that
int number = 2;
x.get<number>()
Any problem with constexpr?
I think the closest you can get is using boost::fusion.
An example would be
#include <boost/fusion/adapted.hpp>
#include <boost/fusion/sequence.hpp>
#include <boost/mpl/int.hpp>
#include <iostream>
class A {
public:
int a;
std::string b;
A() {}
~A() {}
};
BOOST_FUSION_ADAPT_STRUCT(A,
(int, a)
(std::string, b)
)
using namespace boost::fusion;
int main()
{
A x;
x.a = 1;
x.b = "hello";
std::cout << at<boost::mpl::int_<0>>(x) << '\n';
std::cout << at<boost::mpl::int_<1>>(x) << '\n';
at<boost::mpl::int_<0>>(x) = 5;
at<boost::mpl::int_<1>>(x) = std::string("World");
std::cout << at<boost::mpl::int_<0>>(x) << '\n';
std::cout << at<boost::mpl::int_<1>>(x) << '\n';
}
If you want to set several values at the same time when you create the object, you could use a multi-parameter constructor. For example, let's imagine you have this:
class A {
public:
int a;
std::string b;
A() {}
~A() {}
};
You could add a constructor that sets a and b:
class A {
public:
int a;
std::string b;
A() {}
A(int a, std::string b) {
this->a = a;
this->b = b;
}
~A() {}
};
That way, you can create your object and set a and b with :
A a = A(1, "hello");
There is no ready-made way of setting the n-th attribute of your object. You could make one, but I would very, very highly recommend that you don't. Like I said above, if you reorder your attributes, then you will have to rework everything.
If you really, really want to make your life very, very, very much harder, a very ugly and error-prone way of doing this would be like :
template<class T>
void A::setNth(int nth, const T& value) {
switch (nth) {
case 1: a = value; break;
case 2: b = value; break;
// You should #include <stdexcept> to use runtime_error, or you could handle the exception in some other way.
default: throw std::runtime_error("A::setNthAttribute : Value of nth is out of bounds.");
}
}
For the getter:
template<class T>
void A::getNth(int nth, T& valueOut) {
switch (nth) {
case 1: valueOut = a; break;
case 2: valueOut = b; break;
default: throw std::runtime_error("A::getNthAttribute : Value of nth is out of bounds.");
}
}
You would use these methods like this:
A a;
a.setNth(1, 2); // put 2 into a
int i;
a.getNth(1, i); // put a into i
Just writing this code send shivers down my spine. Please, never write what I just wrote. Chuck Norris will kill yoU agfh
86sd asdsa dDASD8!4.
What you are considering is in fact possible, but a bit of a headache. I would approach it by creating a template getter and setter for every member that one can set or get, and then having a template method that takes an int and sets or gets the appropriate property. The getters/setters would have to be specialized for the correct type, and throw an error for other types. This method would have to use a switch to return the right member:
class bar {
private:
int a;
std::string b;
template<T>
T getA() {
// error
}
template<T>
T getB() {
// error
}
template<T>
void setA(const T& A) {
// error
}
template<T>
void setB(const T& B) {
// error
}
template <> std::string getB(); // specialization
template <> int getA();
template <> void setB(const std::string&);
template <> void setA(int);
public:
template<T>
T get(int what) {
switch(what) {
case 1:
return getA();
case 2:
return getB();
default:
// handle error here
break;
}
}
template<T>
void set(int what, const T& t) {
switch(what) {
case 1:
return setA<T>(t);
case 2:
return setB<T>(t);
default:
// handle error here
break;
}
}
};
bar b;
b.set<std::string>(2, "foo");
auto str = b.get<std::string>(2);
Here's an elaborate way to accomplish what you want.
#include <iostream>
#include <string>
// A namespace explicitly defined for class A.
namespace A_NS
{
// A template for members of A.
template <int> struct Member;
// Specialization for the first member.
template <> struct Member<1>
{
using type = int;
type var;
};
// Specialization for the second member.
template <> struct Member<2>
{
using type = std::string;
type var;
};
}
class A {
public:
A() {}
~A() {}
template <int N> typename A_NS::Member<N>::type get() const
{
return static_cast<A_NS::Member<N> const&>(members).var;
}
template <int N> void set(typename A_NS::Member<N>::type const& in)
{
static_cast<A_NS::Member<N>&>(members).var = in;
}
private:
// Define a type for the member variables.
struct Members : A_NS::Member<1>, A_NS::Member<2> {};
// The member variables.
Members members;
};
int main()
{
A a;
a.set<1>(10);
a.set<2>("test");
std::cout << a.get<1>() << ", " << a.get<2>() << std::endl;
}
Output:
10, test
I have created an example to show the language features I am using, the real case is not as simple. The include file is included twice, once for each class. Even though the text is the same, the underlying types are different. In the real case some variables have more initialization than others depending on the class.
Working program
ex1.cpp
#include <iostream>
class one {
public:
one(int a1,int b1) : a(a1), b(b1) {}
int a,b;
#include "ex1.h"
};
class two {
public:
two(double a1,double b1) : a(a1), b(b1) {}
double a,b;
#include "ex1.h"
};
int main()
{
one c1(1,2);
two c2(3.4,5.7);
c1.f();
c2.f();
return 0;
}
ex1.h
void f()
{
std::cout << ( a + b ) << std::endl;
}
I was hoping to change this to a program that uses templates as follows, but I get a compile error.
ex2.cpp
#include <iostream>
class one {
public:
one(int a1,int b1) : a(a1), b(b1) {}
int a,b;
void f();
};
class two {
public:
two(double a1,double b1) : a(a1), b(b1) {}
double a,b;
void f();
};
template<typename X>
void X::f()
{
std::cout << ( a + b ) << std::endl;
}
int main()
{
one c1(1,2);
two c2(3.4,5.7);
c1.f();
c2.f();
return 0;
}
The error message is error: invalid use of template type parameter 'X'
I believe the correct way to do what you want is to use a template to handle the different types present in the class:
#include <iostream>
template<typename X>
class foo {
public:
foo(X a1, X b1) : a(a1), b(b1) {}
X a,b;
void f();
};
template<typename X>
void foo<X>::f()
{
std::cout << ( a + b ) << std::endl;
}
int main()
{
foo<int> c1(1,2);
foo<double> c2(3.4,5.7);
c1.f();
c2.f();
return 0;
}
Generally speaking you don't want to #include code directly into your class to remove code duplication, c++ gives you some better options.
You can see this run here: http://coliru.stacked-crooked.com/a/34d146d7feac8abd
CRTP to the rescue:
template<class D>
struct f_support {
void f() {
// does not block all errors, but catches some:
static_assert( std::is_base_of< f_support, D >::value, "CRTP failure" );
auto* dis = static_cast<D*>(this);
std::cout << ( dis->a + dis->b ) << std::endl;
}
};
then:
class one: public f_support<one> {
...
class two: public f_support<two> {
...
You could move the implementation of f_support<D> into a .cpp file, but that is a hassle.
I would like to thank #Yakk and #shuttle87 for their answers. I took the simplicity of shuttle87's answer and the flexibility of Yakk's answer to come up with this one. Without your help, I would still be using include files. The problem with my solution below is that it requires all classes to have the same number of arguments in their constructor.
#include <iostream>
class one {
public:
one(int a1,int b1) : a(a1), b(b1) {}
int a,b;
void g() { std::cout << "one\t"; }
};
class two {
public:
two(double a1,double b1) : a(a1), b(b1) {}
double a,b;
void g() { std::cout << "two\t"; }
};
template<class X, typename Y>
class three : public X {
public:
three(Y a, Y b) : X(a,b) {}
void f()
{
X::g();
std::cout << ( X::a + X::b ) << std::endl;
}
};
int main()
{
three<one,int> c1(1,2);
three<two,double> c2(3.4,5.7);
c1.f();
c2.f();
return 0;
}