I'm trying to use a custom visitor class for a simple expression grammar.
// .h
class MyVisitor: public MyParserBaseVisitor {...}
// .cpp
Any MyVisitor::visitExpr(MyParser::ExprContext *ctx) {
auto result = visitChildren(ctx);
std::cout<< result.as<int>() << std::endl;
return result;
}
Any MyVisitor::visitInteger(MyParser::IntegerContext *ctx) {
return atoi(ctx->start->getText().c_str());
}
result.as() throws a bad_cast exception and result.isNull() always return true. What am I doing wrong here?
Also, can I use anltrcpp::Any to return a pointer to a custom class object or does it only work with primitive types?
Related
I'm trying to 'touch' an object layout that has a pointer to vTable. I'm getting a virtual function pointer by 2 options:
by traversing pointers from object pointer to virtual table pointer and then to the first function in this table.
simply by taking an address using & operator.
In both cases when I call a function through these pointers I get expected calling results. So in the output I see
Derived func
Derived func
But these function pointers are not equal and I don't know why. So for example
funcDerived = 0x00901ac3
derivedFuncPtr = 0x00901ab9
Does anybody have a thought why they are not equal? I use VS 2017 compiler.
class Base
{
public:
virtual void func()
{
std::cout << "Base func" << std::endl;
}
};
class Derived :public Base
{
public:
virtual void func() override
{
std::cout << "Derived func" << std::endl;
}
};
int main()
{
Derived obj;
auto funcDerived = &Derived::func;
unsigned int* objPtr = reinterpret_cast<unsigned int*>(&obj);
auto derivedVTablePtr = reinterpret_cast<unsigned int*>(*objPtr);
auto derivedFuncPtr = reinterpret_cast<void(*)(Derived*)>(*derivedVTablePtr);
derivedFuncPtr(&obj);
(obj.*funcDerived)();
return 0;
}
Well I have an issue with passing data into a thread using std::thread. I thought I understood the general semantics of copy constructors, etc. but it seems I don't quite grasp the problem. I have a simple class called Log that has hidden it's copy constructor thusly:
class Log
{
public:
Log(const char filename[], const bool outputToConsole = false);
virtual ~Log(void);
//modify behavior
void appendStream(std::ostream *);
//commit a new message
void commitStatus(const std::string str);
private:
//members
std::ofstream fileStream;
std::list<std::ostream *> listOfStreams;
//disable copy constructor and assignment operator
Log(const Log &);
Log & operator=(const Log &);
}
now I have a main based heavily on http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp11/echo/blocking_tcp_echo_server.cpp
int main()
{
static int portNumber = 10000;
Log logger("ServerLog.txt", true);
logger.commitStatus("Log Test String");
try {
boost::asio::io_service ioService;
server(ioService, portNumber, logger);
}
catch (std::exception &e)
{
std::cerr << "Exception " << e.what() << std::endl;
logger.commitStatus(e.what());
}
return 0;
}
You can see that main calls the function server and passes the IOService, portNumber and logger. The logger is passed by reference, thusly:
using boost::asio::ip::tcp;
void server(boost::asio::io_service &ioService, unsigned int port, Log &logger)
{
logger.commitStatus("Server Start");
tcp::acceptor acc(ioService, tcp::endpoint(tcp::v4(), port));
while(true)
{
tcp::socket sock(ioService);
acc.accept(sock);
std::thread newThread(session, &sock, logger);
newThread.detach();
}
logger.commitStatus("Server closed");
}
I get a compiler error when I try to pass the logger (or the socket) to the thread by reference, but I do not get the error when passing it to the session() by reference
static void session(tcp::socket *sock, Log &logger)
{
std::cout << " session () " << std::endl;
}
Now I thought that I understood correctly that a reference is the same as passing a pointer. That is, it does not call the copy constructor, it simply passes the pointer, which it lets you syntactically treat like it's not a pointer.
error C2248: 'Log::Log' : cannot access private member declared in class 'Log'
1> \log.h(55) : see declaration of 'Log::Log'
1> \log.h(28) : see declaration of 'Log'
...
: see reference to function template instantiation 'std::thread::thread(_Fn,_V0_t &&,_V1_t)' being compiled
1> with
1> [
1> Fn=void (_cdecl *)(boost::asio::ip::tcp::socket *,Log &),
1> _V0_t=boost::asio::ip::tcp::socket *,
1> _V1_t=Log &
1> ]
However if I modify it to pass a pointer, everything is happy
...
std::thread newThread(session, &sock, &logger);
...
static void session(tcp::socket *sock, Log *logger)
{
std::cout << " session () " << std::endl;
}
Why is passing by reference calling my copy constructor. Is there something special happening here because of std::thread? Did I misunderstand the copy constructor and pass by reference?
I get a different but equally baffling error if I try to use std::move() as it is done in the example. Is it possible my VS2012 is not implementing C++11 correctly?
std::thread takes its arguments by value. You can get reference semantics back by using std::reference_wrapper:
std::thread newThread(session, &sock, std::ref(logger));
Obviously you must make sure that logger outlives the thread.
I get a compiler error when I try to pass the logger (or the socket) to the thread by reference
It is not sufficient for the thread's entrypoint function to take a reference type: the thread object itself takes its arguments by value. This is because you usually want a copy of objects in a separate thread.
To get around this, you may pass std::ref(logger), which is a reference wrapper hiding reference semantics under a copyable object.
I'd like to have a wrapper thread function, i.e. a function executed by a thread which does some extra stuff, and then calls the user function.
template<class F, class... Args>
void wrapper(F&& user_function, Args&&... args) {
// do some extra stuff
user_function(args); // maybe I need to forward args
// do some extra stuff
}
Ok, this could be a nice wrapper, so I need a manager that uses this wrapper function and allows the user to spawn his own threads:
class ThreadManager {
public:
template<class F, class... Args>
std::thread newThread(F&& f, Args&&... args) {
return std::thread(thread_wrapper<F,Args...>, std::forward<F>(f), std::forward<Args>(args)...);
}
};
this way the thread manager SHOULD spawn a thread that uses the wrapper function which, in turn, does its extra work and calls the user function.
But the compiler now says: Attempt to use a deleted function.
The error is in the thread header:
template <class _Fp, class ..._Args, size_t ..._Indices>
inline _LIBCPP_INLINE_VISIBILITY
void
__thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
{
__invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
}
What am I missing/doing wrong?
[edit]
Using test:
void foo(int i) {
std::cout << "foo: " << i << std::endl;
}
int main(int argc, const char *argv[]) {
ThreadManager mgr;
auto t = mgr.newThread(foo, 10);
t.detach();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
return 0;
}
I'm using Xcode 7.1 with LLVM compiler, but fails on FreeBSD clang 3.3 too.
The Xcode error is:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:337:5: error: attempt to use a deleted function
__invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:347:5: note: in instantiation of function template specialization 'std::__1::__thread_execute' requested here
__thread_execute(*__p, _Index());
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:359:42: note: in instantiation of function template specialization 'std::__1::__thread_proxy >' requested here
int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
I'm not sure what is causing the "Attempt to use a deleted function" in your example, I get other errors related to std::thread's bind mechanism.
It appears the way you are spelling out the template arguments for thread_wrapper is not playing nice with std::thread's constructor - in particular when it uses a simplified std::bind internally. The mix of perfectly forwarded function types and std::decayed function pointers seems to upset std::result_of.
we can make it work by applying some std::decay in newThread ourselves:
return std::thread( thread_wrapper<typename std::decay<F>::type,
typename std::decay<Args>::type...>,
std::forward<F>(f),
std::forward<Args>(args)... );
...but to be honest I'm not entirely sure why that works.
Alternatively, with some indirection and more forwarding, we can avoid having to spell out the template arguments.
We just need a functor that forwards to thread_wrapper (or a polymorphic lambda in C++14):
struct wrapper_helper {
template<class F, class... Args>
void operator()(F&& f, Args&&... args) const {
thread_wrapper(std::forward<F>(f), std::forward<Args>(args)...);
}
};
And use it in newThread:
return std::thread(wrapper_helper{}, std::forward<F>(f), std::forward<Args>(args)...);
Here's the full example showing arguments passed by value, reference and rvalue reference working as intended: http://coliru.stacked-crooked.com/a/b75d5a264f583237
Note: For move-only types like std::unique_ptr, you will definitely want to forward args... in thread_wrapper.
I have the following piece of code:
class Student {
public:
Student(){}
void display() const{}
friend istream& operator>>(istream& is, Student& s){return is;}
friend ostream& operator<<(ostream& os, const Student& s){return os; }
};
int main()
{
Student st;
cin >> st;
cout << st;
getch();
return 0;
}
I have tried myself when omitting the friend keywords to make the operators become the member function of the Student class, then the compiler would produce "binary 'operator >>' has too many parameters". I have read some document saying that happened because all member functions always receive an implicit parameter "this" (that's why all member functions can access private variables).
Based on that explanation, I have tried as follows:
class Student {
public:
Student(){}
void display() const{}
istream& operator>>(istream& is){return is;}
ostream& operator<<(ostream& os){return os; }
};
int main()
{
Student st;
cin >> st;
cout << st;
getch();
return 0;
}
And got the error message: "error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Student' (or there is no acceptable conversion)"
Can anyone give me a clear explanation, please?
You can't say that the function is a friend function, and then include the function in-line. The friend keyword implies that the function is not defined in the class, but it can access all the private and protected variables of the class. Change your code to:
class Student {
public:
Student(){}
void display() const{}
friend istream& operator>>(istream& is, Student& s);
friend ostream& operator<<(ostream& os, const Student& s);
};
istream& operator >>(istream& is, Student& s) { return is; }
ostream& operator <<(ostream& os, const Student& s) { return os; }
Look at http://www.java2s.com/Code/Cpp/Overload/Overloadstreamoperator.htm for another example.
With << and >>, the left hand operand is always a file stream, so you cannot overload them within your actual class (it'd technically have to go in the file stream class).
I forget where that operator is defined, but it would either be the global operator>>, or the operator belonging to the stream.
Defining it in Student is the wrong place.
How do I implement a display function for a binary search tree of strings if display is passed as a function pointer to function inorder?
typedef void (*FunctionType)(TreeItemType& anItem);
void display(TreeItemType& anItem);
void BinarySearchTree::inorder(TreeNode *treePtr,
FunctionType visit)
{ if (treePtr != NULL)
{ inorder(treePtr->leftChildPtr, visit);
visit(treePtr->item);
inorder(treePtr->rightChildPtr, visit);
} // end if
} // end inorder
I've tried to write something like:
cout << anItem;
in the body of display but it doesn't work. TreeItemType is a string so do I need to overload the << operator or = operator to convert from TreeItemType to string? I have researched function pointers and can't figure out how to use:
visit(treePtr->item);
to display the tree's item (which is a string).
int main()
{
BinarySearchTree tree;
TreeItemType item = "20";
tree.searchTreeInsert(item);
tree.inorderTraverse(display); // call to inorderTraverse calls inorder
return 0; // indicates successful completion
} // end function main
void display(TreeItemType& anItem)
{
cout << anItem;
}
Definition of TreeItemType:
typedef string TreeItemType;