I encounter a strange question about rxcpp.
When capturing a rxcpp::connectable_observable variable and call it's connect method, I got a compile error as follow.
1>------ Build started: Project: LearnRx, Configuration: Debug x64 ------
1>Main.cpp
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(19): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(259): error C2662: 'rxcpp::composite_subscription rxcpp::connectable_observable<SourceValue,Multicast>::connect(rxcpp::composite_subscription)': cannot convert 'this' pointer from 'const Result' to 'rxcpp::connectable_observable<SourceValue,Multicast> &'
1> with
1> [
1> SourceValue=int,
1> Multicast=rxcpp::operators::detail::multicast<int,rxcpp::observable<int,rxcpp::sources::detail::create<int,publish_example::<lambda_3a462b77ca22ce68ef79403bfe94c65d>>>,rxcpp::subjects::subject<int>>
1> ]
1> and
1> [
1> Result=rxcpp::connectable_observable<int,rxcpp::operators::detail::multicast<int,rxcpp::observable<int,rxcpp::sources::detail::create<int,publish_example::<lambda_3a462b77ca22ce68ef79403bfe94c65d>>>,rxcpp::subjects::subject<int>>>
1> ]
1> and
1> [
1> SourceValue=int,
1> Multicast=rxcpp::operators::detail::multicast<int,rxcpp::observable<int,rxcpp::sources::detail::create<int,publish_example::<lambda_3a462b77ca22ce68ef79403bfe94c65d>>>,rxcpp::subjects::subject<int>>
1> ]
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(259): note: Conversion loses qualifiers
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(334): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(338): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(345): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(346): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(348): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>Done building project "LearnRx.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The problem code is shown below.
void publish_example() {
auto o = rxcpp::observable<>::create<int>([](rxcpp::subscriber<int> s) {
s.on_next(0);
s.on_next(1);
}).publish();
o.subscribe([](int v) {printf("%d", v); });
rxcpp::observable<>::timer(std::chrono::microseconds(2000)).subscribe(
[](const int i) {},
[](const std::exception_ptr& e) {},
[o]() {
o.connect();
}
);
}
I tried 2 methods to pass compiling
1. Comment "o.connect()"
1. turn variable o to static variable, so o doesn't need to be captured. code is shown follow
void publish_example() {
static auto o = rxcpp::observable<>::create<int>([](rxcpp::subscriber<int> s) {
s.on_next(0);
s.on_next(1);
}).publish();
o.subscribe([](int v) {printf("%d", v); });
rxcpp::observable<>::timer(std::chrono::microseconds(2000)).subscribe(
[](const int i) {},
[](const std::exception_ptr& e) {},
[]() {
o.connect();
}
);
}
Could you help telling me why the compile error appeared and hwo to figure it?
Thank you!
I figured this out.
What happend in here is because connectable_observable::connect is not a const method. After change it to a const method, the compile error disappeared.
Related
I'm trying to use boost::any_range (with random access tag) in some legacy code, but found that it does not like lambdas. Compilation error seems to complain about the lack of default constructor for lambda objects. Is this expected behavior? Is it by design? I'm on MSVC C++17 with boost version 1.66.
Documentation mentions that transformed_range has the same category as input range:
https://www.boost.org/doc/libs/1_66_0/libs/range/doc/html/range/reference/adaptors/reference/transformed.html
The problem is obviously in somewhere in boost::any_range, because if I just use auto, it works (cf rngneg_auto below)
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/any_range.hpp>
#include <iostream>
#include <vector>
using boost::adaptors::transformed;
using double_range = boost::any_range<const double
, boost::random_access_traversal_tag
, const double>;
struct negate
{
constexpr double operator() (const double x) const
{
return -x;
}
};
int main(int argc, char** argv)
{
const std::vector<double> v{ 1.0, 2.0, 3.0 };
// this works
const auto rngneg_auto = v | transformed([](double x) { return -x; });
std::cout << "Second element is " << rngneg_auto[1] << std::endl;
// ... and this
const double_range rngneg = v | transformed(negate());
std::cout << "Second element is " << rngneg[1] << std::endl;
#if 0
// but not that
const double_range rngneg_lambda = v | transformed([](double x) { return -x; });
std::cout << "Second element is " << rngneg_lambda[1] << std::endl;
#endif
return 0;
}
For the first two cases, the program prints
Second element is -2
Second element is -2
Third is a compilation error (I think it's because lambdas aren't default-constructible):
1>\boost.org\boost\1.66.0\include\boost\optional\optional.hpp(733): error C2280: 'main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65> &main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>::operator =(const main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65> &)': attempting to reference a deleted function
1> ***: note: see declaration of 'main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>::operator ='
1>\boost.org\boost\1.66.0\include\boost\optional\optional.hpp(733): note: while compiling class template member function 'void boost::optional_detail::optional_base<T>::assign_value(main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65> &&)'
1> with
1> [
1> T=main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>
1> ]
1>\boost.org\boost\1.66.0\include\boost\optional\optional.hpp(262): note: see reference to function template instantiation 'void boost::optional_detail::optional_base<T>::assign_value(main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65> &&)' being compiled
1> with
1> [
1> T=main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>
1> ]
1>\boost.org\boost\1.66.0\include\boost\optional\optional.hpp(831): note: see reference to class template instantiation 'boost::optional_detail::optional_base<T>' being compiled
1> with
1> [
1> T=main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>
1> ]
1>\boost.org\boost\1.66.0\include\boost\range\detail\default_constructible_unary_fn.hpp(48): note: see reference to class template instantiation 'boost::optional<F>' being compiled
1> with
1> [
1> F=main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>
1> ]
1>\boost.org\boost\1.66.0\include\boost\utility\detail\result_of_iterate.hpp(70): note: see reference to class template instantiation 'boost::range_detail::default_constructible_unary_fn_wrapper<F,R>' being compiled
1> with
1> [
1> F=main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>,
1> R=double
1> ]
1>\boost.org\boost\1.66.0\include\boost\utility\detail\result_of_iterate.hpp(43): note: see reference to class template instantiation 'boost::detail::cpp0x_result_of<F (T0)>' being compiled
1> with
1> [
1> F=const boost::range_detail::default_constructible_unary_fn_wrapper<main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>,double> &,
1> T0=const double &
1> ]
1>\boost.org\boost\1.66.0\include\boost\mpl\eval_if.hpp(41): note: see reference to class template instantiation 'boost::result_of<const UnaryFunc &(const double &)>' being compiled
1> with
1> [
1> UnaryFunc=boost::range_detail::default_constructible_unary_fn_wrapper<main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>,double>
1> ]
1>\boost.org\boost\1.66.0\include\boost\iterator\iterator_adaptor.hpp(154): note: see reference to class template instantiation 'boost::mpl::eval_if<boost::is_same<T,boost::iterators::use_default>,DefaultNullaryFn,boost::mpl::identity<T>>' being compiled
1> with
1> [
1> T=boost::iterators::use_default,
1> DefaultNullaryFn=boost::result_of<const boost::range_detail::default_constructible_unary_fn_wrapper<main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>,double> &(const double &)>
1> ]
1>\boost.org\boost\1.66.0\include\boost\iterator\transform_iterator.hpp(55): note: see reference to class template instantiation 'boost::iterators::detail::ia_dflt_help<Reference,boost::result_of<const UnaryFunc &(const double &)>>' being compiled
1> with
1> [
1> Reference=boost::iterators::use_default,
1> UnaryFunc=boost::range_detail::default_constructible_unary_fn_wrapper<main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>,double>
1> ]
1>\boost.org\boost\1.66.0\include\boost\iterator\transform_iterator.hpp(81): note: see reference to class template instantiation 'boost::iterators::detail::transform_iterator_base<UnaryFunc,Iterator,Reference,Value>' being compiled
1> with
1> [
1> UnaryFunc=boost::range_detail::default_constructible_unary_fn_wrapper<main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>,double>,
1> Iterator=std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<double>>>,
1> Reference=boost::iterators::use_default,
1> Value=boost::iterators::use_default
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.14.26428\include\xutility(652): note: see reference to class template instantiation 'boost::iterators::transform_iterator<boost::range_detail::default_constructible_unary_fn_wrapper<F,R>,It,boost::iterators::use_default,boost::iterators::use_default>' being compiled
1> with
1> [
1> F=main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>,
1> R=double,
1> It=std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<double>>>
1> ]
1>\boost.org\boost\1.66.0\include\boost\iterator\iterator_categories.hpp(120): note: see reference to class template instantiation 'std::iterator_traits<Iterator>' being compiled
1> with
1> [
1> Iterator=boost::iterators::transform_iterator<boost::range_detail::default_constructible_unary_fn_wrapper<main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>,double>,std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<double>>>,boost::iterators::use_default,boost::iterators::use_default>
1> ]
1>\boost.org\boost\1.66.0\include\boost\range\iterator_range_core.hpp(156): note: see reference to class template instantiation 'boost::iterators::iterator_traversal<IteratorT>' being compiled
1> with
1> [
1> IteratorT=boost::iterators::transform_iterator<boost::range_detail::default_constructible_unary_fn_wrapper<main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>,double>,std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<double>>>,boost::iterators::use_default,boost::iterators::use_default>
1> ]
1>\boost.org\boost\1.66.0\include\boost\range\iterator_range_core.hpp(436): note: see reference to class template instantiation 'boost::iterator_range_detail::pure_iterator_traversal<IteratorT>' being compiled
1> with
1> [
1> IteratorT=boost::iterators::transform_iterator<boost::range_detail::default_constructible_unary_fn_wrapper<main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>,double>,std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<double>>>,boost::iterators::use_default,boost::iterators::use_default>
1> ]
1>\boost.org\boost\1.66.0\include\boost\range\adaptor\transformed.hpp(44): note: see reference to class template instantiation 'boost::iterator_range<boost::iterators::transform_iterator<boost::range_detail::default_constructible_unary_fn_wrapper<F,R>,It,boost::iterators::use_default,boost::iterators::use_default>>' being compiled
1> with
1> [
1> F=main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>,
1> R=double,
1> It=std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<double>>>
1> ]
1>***: note: see reference to class template instantiation 'boost::range_detail::transformed_range<main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>,const std::vector<double,std::allocator<_Ty>>>' being compiled
1> with
1> [
1> _Ty=double
1> ]
1>***: note: 'main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65> &main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65>::operator =(const main::<lambda_6f21fa0f6ab2ea01b2b99a85be3eab65> &)': function was explicitly deleted
It's choking because it's trying to figure out how to construct the any_range from the transformed_range and thinks the lambda is a default constructible type. I haven't looked deeply into why but I imagine this is because there's nothing explicitly saying that it isn't and the lambda's type is unique. Of course it's meaningless to try to default construct the lambda's type.
I'd say the simplest way to make it work is to use a type where it's clear (through standard templates) that it can't be default constructed, ie std::function<>. This compiles:
const double_range rngneg_lambda = v | transformed(static_cast<std::function<double(double)>>([](double x) { return -x; }));
Given this c++11 code: (everything remains the same compiling as c++14 or c++17)
class typeSafe {
public:
typeSafe(int tValue)
: value(tValue) {
}
explicit operator int() const {
return value;
}
private:
int value;
};
int main() {
typeSafe x{5};
typeSafe y{5};
if(static_cast<const int&>(x) == static_cast<const int&>(y)) {
}
}
The controversy is because the static_cast attempts converting to a const int&, but the explicit operator gives an int, not an int&.
clang 5.0.0 silently compiles it, with every warning turned on.
gcc rejects it, with:
typeSafe.cpp: In function ‘int main()’:
typeSafe.cpp:17:32: error: invalid static_cast from type ‘typeSafe’ to type ‘const int&’
if(static_cast<const int&>(x) == static_cast<const int&>(y)) {
^
typeSafe.cpp:17:62: error: invalid static_cast from type ‘typeSafe’ to type ‘const int&’
if(static_cast<const int&>(x) == static_cast<const int&>(y)) {
^
Visual Studio 2017 15.5 Preview 2 rejects it, with:
typeSafe.cpp(17): error C2440: 'static_cast': cannot convert from 'typeSafe' to 'const int &'
typeSafe.cpp(17): note: Reason: cannot convert from 'typeSafe' to 'const int'
typeSafe.cpp(17): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Note if the static_cast attempts converting to an int&, then all 3 compilers reject it, with clang saying:
typeSafe.cpp:17:7: error: non-const lvalue reference to type 'int' cannot
bind to a value of unrelated type 'typeSafe'
if(static_cast<int&>(x) == static_cast<int&>(y)) {
^ ~
typeSafe.cpp:17:31: error: non-const lvalue reference to type 'int' cannot
bind to a value of unrelated type 'typeSafe'
if(static_cast<int&>(x) == static_cast<int&>(y)) {
^ ~
So, who's right and who's wrong?
According to microsoft visual studio 2013 (being built from QT creator, QT 5.4), this code is fine:
#include <string>
struct X {
X(std::string const &) {};
};
X wibble() { return ""; }
clang however says
test.cpp(53) : error: no viable conversion from 'const char [1]' to 'X'
X wibble() { return ""; }
test.cpp(49) : note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char [1]' to 'const X &' for 1st argument
struct X {
test.cpp(49) : note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'const char [1]' to 'X &&' for 1st argument
struct X {
test.cpp(50) : note: candidate constructor not viable: no known conversion from 'const char [1]' to 'const std::string &' (aka 'const basic_string<char, char_traits<char>, allocator<char> > &') for 1st argument
X(std::string const &) {};
It does the same thing with QString for what it's worth. Assuming clang is correct, why is it complaining?
It doesn't compile even in MinGW, because it is illegal. Because only one level of user-defined implicit conversion is legal. In your code you have 2 level conversion: to std::string and to X.
struct X {
X( std::string const &) {}
};
X wibble1() { return ""; } // 2 level: error
X wibble2() { return X(""); } // 1 level: ok
Even here you have same error:
void foobar(X x) {
}
int main()
{
foobar(X(""));// ok, 1 level
foobar ("f");
// error: could not convert '(const char*)"f"' from 'const char*' to 'X'
}
I've run into a problem driving me mad. I've implemented a timer using the followin timer entry construct:
typedef std::multimap<boost::posix_time::ptime, events::ITimeout*> TEntryMap;
TEntryMap entries_;
and I insert elements into the multimap with:
boost::posix_time::ptime tTimeout = boost::posix_time::microsec_clock::local_time() + boost::posix_time::milliseconds(timeout);
entries_.insert(std::make_pair(tTimeout, ptr)); // ptr is the events::ITimeout object
and in one translation unit (cpp file) it works perfectly. However, now I need to move this functionality to another cpp file, and now I get a compilation error:
1>Build started 2013-02-07 15:38:18.
1>ClCompile:
1> EventDispatcher.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(260): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const boost::posix_time::ptime' (or there is no acceptable conversion)
1> ....\boost\boost\date_time\posix_time\ptime.hpp(57): could be 'boost::posix_time::ptime &boost::posix_time::ptime::operator =(const boost::posix_time::ptime &)'
1> while trying to match the argument list '(const boost::posix_time::ptime, const boost::posix_time::ptime)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(259) : while compiling class template member function 'std::pair<_Ty1,_Ty2> &std::pair<_Ty1,_Ty2>::operator =(std::pair<_Ty1,_Ty2> &&)'
1> with
1> [
1> _Ty1=const boost::posix_time::ptime,
1> _Ty2=events::ITimeout *
1> ]
1> ....\eventdispatcher.cpp(72) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
1> with
1> [
1> _Ty1=const boost::posix_time::ptime,
1> _Ty2=events::ITimeout *
1> ]
1>
I'm clueless and been struggling with this for 2 hours. I see no difference between the working implementation, and the non-working one. The same construct, same multimap, same includes, same everything! ;(
I found the cause of the problem, and it smells like a VS2010 C++ compiler bug (when using std::remove_if). In a function implementation later on in the class using the construct, I tried to remove elements in the map with:
void removeEntry(events::ITimeout* p)
{
std::remove_if(entries_.begin(), entries_.end(),
[&](TEntryMap::value_type& entry)->bool
{
return (entry.second == p);
}
);
}
Changing that code to:
void removeEntry(events::ITimeout* p)
{
TEntryMap::iterator it = std::find_if(entries_.begin(), entries_.end(),
[&](TEntryMap::value_type& entry)->bool
{
return (entry.second == p);
}
);
if (it != entries_.end()) entries_.erase(it);
}
makes the insertion code work. Go figure. Note that even if I use a struct based predicate instead of a lambda closure, it still doesn't work with std::remove_if...
I tried compiling the following code (From Ogre3d Beginner's Guide By Felix Kerger book)
#include "Ogre\ExampleApplication.h"
class Example1 : public ExampleApplication
{
public:
void createScene()
{Ogre::Entity* ent =
mSceneMgr->createEntity("MyEntity","Sinbad.mesh");
mSceneMgr->getRootSceneNode()->attachObject(ent);
}
};
int main (void)
{
Example1 app;
app.go();
return 0;
}
So the compiled Process didn't work. I am currently using Visual Studio 2010 and OgreSDK_vc10_v1-7-3.
The errors are
1>InitializeBuildStatus:
1> Touching "obj\Debug\OgreApp3.unsuccessfulbuild".
1>ClCompile:
1> main.cpp
1>d:\ogresdk_vc10_v1-7-3\include\ogre\exampleframelistener.h(343): warning C4244: '+=': conversion from 'double' to 'Ogre::Real', possible loss of data
1>d:\ogresdk_vc10_v1-7-3\include\ogre\exampleframelistener.h(344): warning C4244: '-=' : conversion from 'double' to 'Ogre::Real', possible loss of data
1>d:\ogresdk_vc10_v1-7-3\include\ogre\exampleframelistener.h(348): warning C4244: 'argument' : conversion from 'double' to 'Ogre::Real', possible loss of data
1>d:\ogresdk_vc10_v1-7-3\include\ogre\exampleframelistener.h(349): warning C4244: 'argument' : conversion from 'double' to 'Ogre::Real', possible loss of data
1>d:\ogresdk_vc10_v1-7-3\include\ogre\exampleframelistener.h(454): warning C4244: '-=' : conversion from 'double' to 'Ogre::Real', possible loss of data
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain#16 referenced in function ___tmainCRTStartup
1>bin\Debug\\OgreApp3.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:10.57
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Replace the signature of your main function to the following:
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
right bottom on your project
->property linker->subsystem
change your subsystem with "CONSOLE(/SUBSYSTEM:CONSOLE)"
like this https://www.dropbox.com/s/li3qnzw152snizo/ogre.png
Your compiler thinks the program is a windows application but your source code is for a command line application.