std::ref error in VS2015 - c++

#include <thread>
#include <iostream>
#include <functional>
struct C
{
void printMe() const
{}
};
struct D
{
void operator()() const
{}
};
int main()
{
D d;
std::thread t9(std::ref(d)); // fine
t9.join();
C c;
std::thread t8(&C::printMe, std::ref(c)); // error in VS2015
t8.join();
/*
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(238): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(238): note: With the following template arguments:
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(238): note: '_Callable=void (__thiscall C::* )(void) const'
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(238): note: '_Types={std::reference_wrapper<C>}'
*/
}
http://ideone.com/bxXJem built without problems
Is the following code correct?
std::thread t8(&C::printMe, std::ref(c));

No, it doesn't compile. To compile it and make run it you need:
1) It needs to set the method printMe as a static method to send an its address (printMe's address), otherwise you are sending a relative address to instance C.
2) At the moment of create the thread t8, you are sending a reference to object C as an argument but the function printMe doesn't have arguments, So you need declare the argument into the printMe method.
3) As #cpplearner told you, send the pointer of the method as: std::thread t8(&C::printMe, &c);.
The resulting code is:
#include <thread>
#include <iostream>
#include <functional>
struct C
{
static void printMe(C* ref)
{
std::cout << "Address of C ref: " << std::hex << ref << std::endl;
}
};
struct D
{
void operator()() const
{
std::cout << "Operator D" << std::endl;
}
};
int main()
{
D d;
std::thread t9(std::ref(d)); // fine
t9.join();
C c;
std::thread t8(&C::printMe, &c); // Compile in VS2015
t8.join();
std::cout << "END" << std::endl;
}
And the output is:

Related

std::thread constructor producing a MSVC C2661 compiler error via std::tuple's constructor

I was following a talk on YouTube by Kelvin Henney based on the idiom of Functional C++... About 50 minutes into the video he starts showing an example class structure that he named channel. Then he writes the simple fizzbuzz function and is going to pass that into a server like piece of code using threads. I'm using the code from his video which can be found here: Kevlin Henney - Functional C++
However, when I try to compile the program, Visual Studio is generating the C2661 compiler error pointing to std::tuple... which is coming from the std::tread's constructor within my code.
main.cpp
#include <iostream>
#include <exception>
#include <string>
#include <thread>
#include "server.h"
std::string fizzbuzz(int n) {
return
n % 15 == 0 ? "FizzBuzz" :
n % 3 == 0 ? "Fizz" :
n % 5 == 0 ? "Buzz" :
std::to_string(n);
}
void fizzbuzzer(channel<int> & in, channel<std::string> & out) {
for (;;)
{
int n;
in.receive(n);
out.send(fizzbuzz(n));
}
}
int main() {
try {
channel<int> out;
channel<std::string> back;
std::thread fizzbuzzing(fizzbuzzer, out, back);
for (int n = 1; n <= 100; ++n) {
out << n;
std::string result;
back >> result;
std::cout << result << "\n";
}
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
server.h
#pragma once
#include <condition_variable>
#include <queue>
#include <mutex>
#include <iostream>
template<typename ValueType>
class receiving;
template<typename ValueType>
class channel {
private:
std::mutex key;
std::condition_variable_any non_empty;
std::queue<ValueType> fifo;
public:
void send(const ValueType & to_send) {
std::lock_guard<std::mutex> guard(key);
fifo.push(to_send);
non_empty.notify_all();
}
bool try_receive(ValueType & to_receive) {
bool received = false;
if (key.try_lock()) {
std::lock_guard<std::mutex> guard(key, std::adopt_lock);
if (!fifo.empty()) {
to_receive = fifo.front();
fifo.pop();
received = true;
}
}
return received;
}
void receive(ValueType & to_receive) {
std::lock_guard<std::mutex> guard(key);
non_empty.wait(
key,
[this] {
return !fifo.empty();
});
to_receive = fifo.front();
fifo.pop();
}
void operator<<(const ValueType & to_send) {
send(to_send);
}
receiving<ValueType> operator>>(ValueType & to_receive) {
return receiving(this, to_receive);
}
};
template<typename ValueType>
class receiving {
private:
channel<ValueType> * that;
ValueType & to_receive;
public:
receiving(channel<ValueType> * that, ValueType & to_receive)
: that(that), to_receive(to_receive)
{}
receiving(receiving && other)
: that(other.that), to_receive(other.to_receive)
{
other.that = nullptr;
}
operator bool() {
auto from = that;
that = nullptr;
return from && from->try_recieve(to_receive);
}
~receiving() {
if (that)
that->receive(to_receive);
}
};
I know that the code he is showing is only an example code, but I figured I would try it within my IDE while following the video to get a better understanding of his talk. I'd like to be able to compile this just to see the generated output, and to be able to step through the debugger and disassembler, but I've hit a roadblock at this point. I understand the generated compiler error, just not sure how to resolve it based on his code sample...
Here's the compiler error that is being generated:
1>------ Build started: Project: Computations, Configuration: Debug Win32 ------
1>main.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\memory(2539): error C2661: 'std::tuple<void (__cdecl *)(channel<int> &,channel<std::string> &),channel<int>,channel<std::string>>::tuple': no overloaded function takes 3 arguments
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\thread(46): note: see reference to function template instantiation 'std::unique_ptr<std::tuple<void (__cdecl *)(channel<int> &,channel<std::string> &),channel<int>,channel<std::string>>,std::default_delete<_Ty>> std::make_unique<std::tuple<void (__cdecl *)(channel<int> &,channel<std::string> &),channel<int>,channel<std::string>>,void(__cdecl &)(channel<int> &,channel<std::string> &),channel<int>&,channel<std::string>&,0>(void (__cdecl &)(channel<int> &,channel<std::string> &),channel<int> &,channel<std::string> &)' being compiled
1> with
1> [
1> _Ty=std::tuple<void (__cdecl *)(channel<int> &,channel<std::string> &),channel<int>,channel<std::string>>
1> ]
1>c:\users\skilz99\source\repos\computations\computations\main.cpp(31): note: see reference to function template instantiation 'std::thread::thread<void(__cdecl &)(channel<int> &,channel<std::string> &),channel<int>&,channel<std::string>&,void>(_Fn,channel<int> &,channel<std::string> &)' being compiled
1> with
1> [
1> _Fn=void (__cdecl &)(channel<int> &,channel<std::string> &)
1> ]
1>Done building project "Computations.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Failed to compile while the argument is lambda. [using hippomocks]

I wrote an unit test with hippomocks, but got error while compiling it.
The compiler is VS 2010.
How can I fix it ?
#include "hippomocks.h"
#include <functional>
using namespace HippoMocks;
struct A
{
virtual void f(std::function<void (int)> arg);
};
int main(void)
{
MockRepository mock;
A* aptr = mock.Mock<A>();
mock.ExpectCall(aptr, A::f); // error
return 0;
}
The output is :
main.cpp
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : wa
rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
pecify /EHsc
c:\users\cong\project\test\test\hippomocks.h(466) : error C2593: 'operator <<' i
s ambiguous
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\ostream(2
06): could be 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Trai
ts>::operator <<(std::_Bool)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\users\cong\project\test\test\hippomocks.h(441): or 'std::ostrea
m &HippoMocks::operator <<(std::ostream &,const HippoMocks::NotPrintable &)'
while trying to match the argument list '(std::ostream, std::tr1::functi
on<_Fty>)'
with
[
_Fty=void (int)
]
c:\users\cong\project\test\test\hippomocks.h(463) : while compiling clas
s template member function 'void HippoMocks::printArg<T>::print(std::ostream &,T
,bool)'
with
[
T=std::tr1::function<void (int)>
]
c:\users\cong\project\test\test\hippomocks.h(614) : see reference to cla
ss template instantiation 'HippoMocks::printArg<T>' being compiled
with
[
T=std::tr1::function<void (int)>
]
Enhancing #dascandy's comment here is how his method can look like. Place it after including hippomocks.h:
template<>
struct printArg<std::function<void (int)> >
{
static inline void print(std::ostream &os, std::function<void (int)> arg, bool withComma)
{
if (withComma)
{
os << ",";
}
if (arg)
{
os << "true";
}
else
{
os << "false";
}
}
};
Note that I didn't test this very example but rather took our solution and adapted the type to the original post's example. I'd be happy to know whether this works for you.

error C2065: undeclared identifier in template function

I'm a newbie in C++ and i'm trying to put a little project on wheels, but i'm having a hard time with those errors and i don't know what i'm doing wrong. I think, i've included all neccesary header files.
What i'm doing wrong?
Thank you in advance!
d:\files\visual studio 2010\projects\calculator\aplicatie.cpp(31): error C2065: 'CmdAritmetice' : undeclared identifier
d:\files\visual studio 2010\projects\calculator\aplicatie.cpp(31): error C2062: type 'int' unexpected
d:\files\visual studio 2010\projects\calculator\aplicatie.cpp(37): error C2065: 'cmd1' : undeclared identifier
d:\files\visual studio 2010\projects\calculator\aplicatie.cpp(37): error C2228: left of '.Execute' must have class/struct/union
type is ''unknown-type''
Edit
If i modify CmdAritmetice <int, Suma> cmd1("+"); with UI::CmdAritmetice<Calcule::Suma<int>> cmd1("+"); the following errors appear when try to compile:
1>d:\files\visual studio 2010\projects\calculator\aplicatie.cpp(32): error C2440: 'specialization' : cannot convert from 'T (__cdecl *)(T,T)' to 'int (__cdecl *)(int,int)'
1> None of the functions with this name in scope match the target type
1>d:\files\visual studio 2010\projects\calculator\aplicatie.cpp(39): error C2660: 'Calculator::UI::CmdAritmetice<Operatie>::Execute' : function does not take 1 arguments
1> with
1> [
1> Operatie=0x0
1> ]
Main.cpp
#include "aplicatie.h"
using namespace Calculator;
int main()
{
Aplicatie app;
app.Run();
return 0;
}
aplicatie.cpp edited
#include <iostream>
#include "aplicatie.h"
#include "Calcule\operatii.h"
#include "UI\cmdaritmetice.h"
using namespace std;
namespace Calculator{
(...)
void Aplicatie::Run()
{
TestSuma();
CmdAritmetice <int, Suma> cmd1("+"); //here i have errors
cmd1.Execute("Introduceti doua numere intregi (x, y)");
}
void Aplicatie::TestSuma()
{
int x = 10, y = 20;
int r = Calcule::Suma(x,y);
}
}
cmdaritmetice.h
#ifndef ARITMETICE_H
#define ARITMETICE_H
#include "UI\comanda.h"
namespace Calculator{
namespace UI{
template<int Operatie(int, int)>
class CmdAritmetice : public ComandaCalcule
{
public:
CmdAritmetice(const string &nume) : ComandaCalcule(nume)
{
}
void Execute()
{
cout << Nume() << endl;
cout << "Introduceti doua numere intregi (x, y)\n";
int x, y;
cin >> x >> endl;
cin >> y >> endl;
cout << x << " " << Nume() << " " << y << " = " << Operatie (x,y) <<endl;
}
};
}
}
#endif
LE
**operatii.h**
#ifndef OPERATII_H
#define OPERATII_H
namespace Calculator{
namespace Calcule{
template<typename T>
T Suma(T x, T y)
{
return x + y;
}
}
#endif
CmdAritmetice is defined in namespace Calculator::UI while Aplicatie is defined in namespace Calculator. So, you must refer to CmdAritmetice as UI::CmdAritmetice within the Calculator namespace.
As for error saying Suma cannot be found, you haven't posted the definition of Suma, so it's hard to say what's going on there, but check to make sure that that too isn't defined within some nested namespace.
EDIT:
The definition for CmdAritmetice is
template<int Operatie(int, int)>
class CmdAritmetice : public ComandaCalcule
{
// ...
};
So it takes a template argument that is function with the signature int(int, int), i.e. a function that accepts 2 ints and returns an int. Within Aplicatie::Run() you try to instantiate the class as
CmdAritmetice <int, Suma>
Obviously this will not work, the template argument needs to be a function matching the signature you've specified in the definition. Also, references to Suma need to be qualified as Calcule::Suma.
I think what you intend to do is
UI::CmdAritmetice<Calcule::Suma<int>> cmd1("+");

unable to be compiled

#include <iostream>
using namespace std;
template < class T >
void swap (T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
int main ()
{
char a = 'a';
char b = 'b';
swap (a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
the code can not be compiled under linux KDE command line (gcc compiler).
however if I change "using namespace std" into "using std::cout; using std::cin using std::endl" the program can be compiled and run well. what's wrong with it?
Thank you very much
Your definition of swap is conflicting with the already existing definition of std::swap when you bring std in the global namespace using using namespace.
Note that the conflict occurs when you try to instantiate the template, you can use
::swap (a, b);
to choose explicitly your definition.
Here is what VC++ says:
error C2668: 'swap' : ambiguous call to overloaded function
1> c:\lisp\other\test_meth\test_meth.cpp(7): could be 'void swap<char>(T &,T &)'
1> with
1> [
1> T=char
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(100): or 'void std::swap<char>(_Ty &,_Ty &)'
1> with
1> [
1> _Ty=char
1> ]
1> while trying to match the argument list '(char, char)'
The problem is: std namespace already contains the template function swap.

What causes C4250 (class inherits member via dominance) when using boost serialization with a virtual base class?

The meaning of the VC++ compiler warning C4250 'class1' : inherits 'class2::member' via dominance is clear to me. (But see here for an explanation.)
I have currently the problem that I get this warning when serializing a class hierarchy that has an abstract base class with boost::serialization (1.44.0).
Please note that my classes do not form any kind of diamond-like inheritance hierarchy that could cause this warning, but the warning is caused by the instantiation of boost::detail::is_virtual_base_of_impl<...> when serializing instances of derived classes. (Which seems to be using is_virtual_base_of from Boost.TypeTraits.)
Here is a minimal code sample to reproduce the warning on Visual Studio 2005. Note that the code should be dropped as-is into one cpp-file and it should compile.
Note also the two points in the code that I have marked by comments that trigger the warning. If BOOST_CLASS_EXPORTis not used then the warning is not triggerd, but more interestingly the warning is also not triggered, if the derived class does not use virtual inheritance! (So maybe I do not understand C4250 after all.)
// -- std includes --
#include <iostream>
#include <sstream>
#include <string>
// -- boost serialization --
#define BOOST_SERIALIZATION_DYN_LINK
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
// Base with serialization support
struct Base
{
virtual ~Base() {};
virtual void DoStuff() const {
std::cout << "Base#[" << static_cast<const void*>(this) << "]::DoStuff() called\n";
}
template<class Archive> // serialization support!
void serialize(Archive & ar, const unsigned int file_version) { /*empty*/ }
};
// (The only) Specific class with ser. support
struct Concrete2 : virtual/*!C4250!*/ public Base
{
virtual void DoStuff() const {
std::cout << "Concrete2#[" << static_cast<const void*>(this) << "]::DoStuff() called\n";
}
template<class Archive> // serialization support!
void serialize(Archive & ar, const unsigned int ver) {
ar & boost::serialization::base_object<Base>(*this);
// This is just a test - no members neccessary
std::cout << "Concrete2::serialize!" << typeid(ar).name() << "\n";
}
};
// Without guid export -> *no* C4250, even *with* virtual inheritance
// (however, can't be serialized via base class pointer anymore)
BOOST_CLASS_EXPORT(Concrete2);
BOOST_CLASS_TRACKING(Concrete2, boost::serialization::track_never);
int main() {
using namespace std;
Concrete2 obj1;
obj1.DoStuff();
// The following test code is not neccessary to generate the warning ...
// (but is neccessary to show if base-pointer serialization works at runtime)
Base* ref1 = &obj1;
ostringstream out_buf;
boost::archive::text_oarchive out_archive(out_buf);
out_archive << ref1;
const string buf = out_buf.str();
cout << "Serialized obj:\n~~~~\n";
cout << buf;
cout << "\n~~~~~\n";
std::istringstream in_buf(buf);
boost::archive::text_iarchive in_archive(in_buf);
// Concrete2 obj2;
Base* ref2;
in_archive >> ref2;
if(ref2)
ref2->DoStuff();
delete ref2;
}
And here is the compiler warning (ugh!):
1>...\boost_library-1_44_0\boost\type_traits\is_virtual_base_of.hpp(61) : warning C4250: 'boost::detail::is_virtual_base_of_impl<Base,Derived,tag>::X' : inherits 'Concrete2::Concrete2::DoStuff' via dominance
1> with
1> [
1> Base=type,
1> Derived=Concrete2,
1> tag=boost::mpl::bool_<true>
1> ]
1> ...\boostserializewarningtest\vbc.cpp(27) : see declaration of 'Concrete2::DoStuff'
...
1> ...\boost_library-1_44_0\boost\mpl\eval_if.hpp(40) : see reference to class template instantiation 'boost::mpl::if_<T1,T2,T3>' being compiled
1> with
1> [
1> T1=boost::is_virtual_base_of<type,Concrete2>,
1> T2=boost::mpl::identity<boost::serialization::void_cast_detail::void_caster_virtual_base<Concrete2,type>>,
1> T3=boost::mpl::identity<boost::serialization::void_cast_detail::void_caster_primitive<Concrete2,type>>
1> ]
1> ...\boost_library-1_44_0\boost\serialization\void_cast.hpp(279) : see reference to class template instantiation 'boost::mpl::eval_if<C,F1,F2>' being compiled
1> with
1> [
1> C=boost::is_virtual_base_of<type,Concrete2>,
1> F1=boost::mpl::identity<boost::serialization::void_cast_detail::void_caster_virtual_base<Concrete2,type>>,
1> F2=boost::mpl::identity<boost::serialization::void_cast_detail::void_caster_primitive<Concrete2,type>>
1> ]
1> ...\boost_library-1_44_0\boost\serialization\base_object.hpp(68) : see reference to function template instantiation 'const boost::serialization::void_cast_detail::void_caster &boost::serialization::void_cast_register<Derived,Base>(const Derived *,const Base *)' being compiled
1> with
1> [
1> Derived=Concrete2,
1> Base=type
1> ]
...
1> ...\boost_library-1_44_0\boost\serialization\export.hpp(128) : while compiling class template member function 'void boost::archive::detail::`anonymous-namespace'::guid_initializer<T>::export_guid(boost::mpl::false_) const'
1> with
1> [
1> T=Concrete2
1> ]
1> ...\boostserializewarningtest\vbc.cpp(40) : see reference to class template instantiation 'boost::archive::detail::`anonymous-namespace'::guid_initializer<T>' being compiled
1> with
1> [
1> T=Concrete2
1> ]
The reason is in fact the is_virtual_base_of check from boost type traits. This check-construct will generate warning C4250 if the check is successful, as can be seen by this example:
...
struct base {
virtual void mf() { };
};
struct derived_normal : public base {
virtual void mf() { };
};
struct derived_virt : virtual public base {
virtual void mf() { };
};
int main() {
using namespace std;
cout << "boost::is_virtual_base_of<base, derived_normal>::value reports: ";
// The following line DOES NOT cause C4250
cout << boost::is_virtual_base_of<base, derived_normal>::value << endl;
cout << "boost::is_virtual_base_of<base, derived_virt> reports: ";
// The following line causes C4250:
cout << boost::is_virtual_base_of<base, derived_virt>::value << endl;
...
FWIW, the usage of this type-traits tool in boost serialization goes like this:
macro BOOST_EXPORT_CLASS ->
macro BOOST_CLASS_EXPORT_IMPLEMENT ->
struct guid_initializer (in export.hpp) ->
(...) void_cast.hpp / void_cast_register -> is_virtual_base_of is used here
As far as I can tell the warning is completely harmless in this case and can be prevented by wrapping the header in:
#pragma warning( push )
#pragma warning( disable : 4250 ) // C4250 - 'class1' : inherits 'class2::member' via dominance
#include ...
#pragma warning( pop ) // C4250