boost::phoenix::static_cast_ compile errors - c++

I'm encountering a compile error when trying to static cast an argument inside of a boost phoenix lambda. The errors themselves are way too long, so I've posted them to pastebin here.
I've created a minimal example showing what I am trying to do. If I make the variable b into an A pointer and thusly don't cast, then everything compiles and runs correctly. Does anyone know why this is happening?
Minimal example (compile with "clang++ -lboost_thread phoenix_test.cpp"):
#include <boost/phoenix.hpp>
#include <iostream>
using namespace boost;
using namespace phoenix;
using namespace arg_names;
using namespace local_names;
class A
{
public:
A(int a)
: mA(a)
{};
int GetX() const {return mA;};
protected:
int mA;
};
class B : public A
{
public:
B(int a)
: A(a)
{};
int GetX() const { return mA + 1; }
};
int main (void)
{
const A* a = new A(3);
const A* b = new B(2);
BOOST_AUTO(test, (_1->*&A::GetX)() + (static_cast_<const B*>(_2)->*&B::GetX)());
std::cout << test(a, b) << std::endl;
delete a;
delete b;
return 0;
}
The compiler used was clang 3.4, and gcc 4.6.3 was also tried.

The error messages seem to imply that you were using dynamic_cast_ there.
Dynamic casts require virtual classes. Add a virtual member to the base class (e.g. the destructor)
Sample:
Live On Coliru
#include <boost/phoenix.hpp>
#include <iostream>
namespace px = boost::phoenix;
using namespace px::arg_names;
using namespace px::local_names;
class A {
public:
virtual ~A() {}
A(int a) : mA(a){};
int GetX() const { return mA; };
protected:
int mA;
};
class B : public A {
public:
B(int a) : A(a){};
int GetX() const { return mA + 1; }
};
int main()
{
const A* a = new A(3);
const A* b = new B(2);
BOOST_AUTO(test, (_1->*&A::GetX)() + (px::dynamic_cast_<const B*>(_2)->*&B::GetX)());
std::cout << test(a, b) << std::endl;
delete a;
delete b;
return 0;
}
Prints
6

Related

What syntax should i use for member initializers?

What syntax should i use for member initializers in c++?
I know how to use it for Attribute but not for functions.
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(int a, int b,int c):
regVar(a), constVar(b),function(c)
{}
public:
int regVar;
const int constVar;
void function(int a){
cout << a;
}
};
int main(){
MyClass myclass(10,20);
cout << myclass.regVar;
}
Thanks in advance
I tried function(c), c.function etc.

Problems with copy constructor and vector

I met a problem with copy constructor and vector.
#include <iostream>
#include <vector>
using namespace std;
class B {
private:
int val;
};
class A {
private:
B b;
public:
A() {}
A(A& a) {}
};
int main(int argc, char const *argv[])
{
A aa;
A a2 = aa;//it works well
//addA(va, aa);
return 0;
}
But when I do something like this:
#include <iostream>
#include <vector>
using namespace std;
class B {
private:
int val;
};
class A {
private:
B b;
public:
A() {}
A(A& a) {}
};
void addA(std::vector<A>& va, A a) {
va.push_back(a);
}
int main(int argc, char const *argv[])
{
std::vector<A> va;
A aa;
A a2 = aa;
addA(va, aa); // I got a compile error
return 0;
}
there is a compiler error.
But if I change the args in class A's copy constructor to const A&, it works well.
Why is this so?
The argument to std::vector<A>::push_back is const A &, so when the vector tries to copy that into the storage it allocated for the element, it requires a constructor that takes const A &. Because yours doesn't, it can't be used.

Basic Class Inheritance in C++

I am typing the following code and I am getting the following error at line-1
[Error] no matching function for call to 'int_adder::add()
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.add(); //LINE-1
cout << ia.add(10, 20); //LINE-2
return 0;
}
As pointed by others in the comment, I have corrected it:-
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.adder::add(); //LINE-1
cout << ia.add(10, 20); //LINE-2
return 0;
}
The statement adder::add() will overide the function add() present in int_adder.
Can't fing exact dupe, but you can make overloads from base class visible by using using directive, example:
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
using adder::add; // expose base class overload as our own
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.add(); //LINE-1
ia.adder::add(); // explicit name also works
cout << ia.add(10, 20); //LINE-2
return 0;
}
As the other answer mentions using base class name scope also works. It all depends on your needs and class design.
Basically defining an overload in a derived class prevents implicit method look up from matching base class overloads, so you have to be explicit about it in one way or another.
You have totally two different versions of add in the first place. You are not overriding, you are overloading. You are just providing a new add function that has nothing to do with the other add function of the parent.
So first of all, do you want to override or overload?
Overriding the parent add would like the following:
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
int add() override {cout <<"int_adder ::add() ";};
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.adder::add(); //LINE-1 <- would work displays adder::add()'s message
cout << ia.add(10, 20); //LINE-2
adder ia = int_adder{}; // now this is interesting
ia.add(); // would work displays adder::add()'s message - cause you did not ask for virtuality
return 0;
}

Any Example that Prove Initializer List Call before Constructor C++

I have searched but still didn't get easy and proper answer, Below is my code.
#include <iostream>
using namespace std;
class Parent
{
private:
int a;
public:
Parent():a(3) { cout << a; }
};
int main()
{
Parent obj;
return 0;
}
Can you add additional lines of code that can prove or show me that initializer list call before constructor?
I would modify you code ever so slightly:
#include <iostream>
using namespace std;
class Parent
{
public:
int a;
public:
Parent():a(3){
a = 4;
}
};
int main()
{
Parent obj;
cout << obj.a;
return 0;
}
The output is 4, thus a was initialized with 3 and then assigned 4.
Simply add data member, which has constructor, which prints something. Example:
#include <iostream>
using namespace std;
struct Data {
Data(int a) {
cout << "Data constructor with a=" << a << endl;
}
};
class Parent
{
private:
Data a;
public:
Parent():a(3){
cout << "Parent constructor" << endl;
}
};
int main()
{
Parent obj;
return 0;
}
Output:
Data constructor with a=3
Parent constructor
Conclusion: Data constructor was called before constructor body of Parent.
You don't get variable "a" value 10 in this program , a assigned before constructor method called .
#include<iostream>
using namespace std;
class Test
{
public:
int a,b;
public:
Test():a(b){
b=10;
}
};
int main()
{
Test obj;
cout<<"a :"<<obj.a<<" b:"<<obj.b;
return 0;
}
This is best shown with multiple classes:
#include <iostream>
class A
{
public:
A()
{
std::cout << "Hello World!" << std::endl;
}
};
class B
{
private:
A* a;
public:
// Call a's constructor
B():a(new A)
{
// Some code
}
~B()
{
delete a;
}
};
int main()
{
B obj;
return 0;
}

C++ : unresolved overloaded function when using function pointers

#include <iostream>
using namespace std;
class B
{
public:
int getMsg(int i)
{
return i + 1;
}
};
class A
{
B b;
public:
void run()
{
taunt(b.getMsg);
}
void taunt(int (*msg)(int))
{
cout << (*msg)(1) << endl;
}
};
int main()
{
A a;
a.run();
}
The above code has a class B inside a class A, and class A has a method taunt that takes a function as an argument. class B's getMsg is passed into taunt...The above code generated the following error message: "error: no matching function for call to 'A::taunt()'"
What's causing the error message in the above code? Am I missing something?
Update:
#include <iostream>
using namespace std;
class B
{
public:
int getMsg(int i)
{
return i + 1;
}
};
class A
{
B b;
public:
void run()
{
taunt(b.getMsg);
}
void taunt(int (B::*msg)(int))
{
cout << (*msg)(1) << endl;
}
};
int main()
{
A a;
a.run();
}
t.cpp: In member function 'void A::run()':
Line 19: error: no matching function for call to 'A::taunt()'
compilation terminated due to -Wfatal-errors.
I'm still getting the same error after changing (*msg)(int) to (B::*msg)(int)
b.getMsg is not the correct way to form a pointer to member, you need &B::getMsg.
(*msg)(1) is not the correct way to call a function through a pointer to member you need to specify an object to call the function on, e.g. (using a temporary) (B().*msg)(1).
The right way to do such things in OOP is to use interfaces so all you need to do is to define an interface and implement it in B class after that pass the pointer of instance which implements this interface to your method in class A.
class IB{
public:
virtual void doSomething()=0;
};
class B: public IB{
public:
virtual void doSomething(){...}
};
class A{
public:
void doSomethingWithB(IB* b){b->doSomething();}
};
This works in VS 2010. The output is the same on all lines:
#include <iostream>
#include <memory>
#include <functional>
using namespace std;
using namespace std::placeholders;
class A
{
public:
int foo(int a, float b)
{
return int(a*b);
}
};
int main(int argc, char* argv[])
{
A temp;
int x = 5;
float y = 3.5;
auto a = std::mem_fn(&A::foo);
cout << a(&temp, x, y) << endl;
auto b = std::bind(a, &temp, x, y);
cout << b() << endl;
auto c = std::bind(std::mem_fn(&A::foo), &temp, _1, y);
cout << c(5) << endl;
}
Basically, you use std::mem_fn to get your callable object for the member function, and then std::bind if you want to bind additional parameters, including the object pointer itself. I'm pretty sure there's a way to use std::ref to encapsulate a reference to the object too if you'd prefer that. I also included the _1 forwarding marker just for another way to specify some parameters in the bind, but not others. You could even specify everything BUT the class instance if you wanted the same parameters to everything but have it work on different objects. Up to you.
If you'd rather use boost::bind it recognizes member functions and you can just put it all on one line a bit to be a bit shorter: auto e = boost::bind(&A::foo, &temp, x, y) but obviously it's not much more to use completely std C++11 calls either.