jeudi 31 janvier 2019

compile error in reactive extensions for cpp

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!

Aucun commentaire:

Enregistrer un commentaire