ReactiveX / RxCpp

Reactive Extensions for C++
Apache License 2.0
3.06k stars 395 forks source link

example code for rxcpp::observable<>::create<int>(... doesnt compile in GCC 10.2.0 (c++17) #548

Open chris-stones opened 4 years ago

chris-stones commented 4 years ago

Not tried in other compilers.
But in GCC 10.2.0 (Manjaro GNU/Linux), the following code - offered in many learning resources does not compile.

Is this a bug in RxCPP, or GCC?

main.cpp

#include "rxcpp/rx-observable.hpp"

int main() {

    auto ints = rxcpp::observable<>::create<int>(
            [](rxcpp::subscriber<int> s){
                s.on_next(1);
                s.on_next(2);
                s.on_completed();
            });
    ints.
            subscribe(
            [](int v){printf("OnNext: %d\n", v);},
            [](){printf("OnCompleted\n");});

    return 0;
}

CMake

cmake_minimum_required(VERSION 3.17)
project(RxCPPPG)

set(CMAKE_CXX_STANDARD 17)

add_executable(RxCPPPG main.cpp)

target_include_directories(RxCPPPG PUBLIC libs/RxCpp/Rx/v2/src)

console

[ 50%] Building CXX object CMakeFiles/RxCPPPG.dir/main.cpp.o
/home/chris/CLionProjects/RxCPPPG/main.cpp: In function ‘int main()’:
/home/chris/CLionProjects/RxCPPPG/main.cpp:6:38: error: ‘create’ is not a member of ‘rxcpp::observable<void, void>’
    6 |     auto ints = rxcpp::observable<>::create<int>(
      |                                      ^~~~~~
/home/chris/CLionProjects/RxCPPPG/main.cpp:6:45: error: expected primary-expression before ‘int’
    6 |     auto ints = rxcpp::observable<>::create<int>(
      |                                             ^~~
/home/chris/CLionProjects/RxCPPPG/main.cpp:11:14: error: expected ‘,’ or ‘;’ before ‘)’ token
   11 |             });

RxCPP version 9002d9bea0e6b90624672e90a409b56de5286fc6.

chris-stones commented 4 years ago

The create test uses a similar code, but passes the observer by reference.

return rx::observable<>::create<int>(
                        [&](const rx::subscriber<int>& s){
                            invoked++;
                            s.on_next(1);
                            s.on_next(2);
                        })

This doesnt compile either.
Im very confused by this all!

The rxcppv2_test target, which includes this source DOES compile...
But when i try to build the single test for create, it does not compile..Same error as above.

Looking at the actual code for create(), it LOOKS like it should be used something like this.. ?

return rx::sources::create<int>(
                        [&](const rx::subscriber<int>& s){
                            invoked++;
                            s.on_next(1);
                            s.on_next(2);
                        })

In the sources namespace, NOT obseravable<> struct.

Using the sources namespace instead of observable<> seems to fix this for me... Which leads me to belive that perhaps the documentation is hopelessly out of date?
But, then why would the create test not raise this as an issue ?

Is there something clever i need to do to make observable<>::create work?
Im affraid im having a little trouble understanding the code.. It is all very clever.