wqking / eventpp

Event Dispatcher and callback list for C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CallbackList tutorial 4, for each couldn't compile

linhanwang opened this issue · comments

I tried to compile the following code in the tutorial but failed.
`using CL = eventpp::CallbackList<void ()>;
CL callbackList;

// Add some callbacks.
callbackList.append( {
std::cout << "Got callback 1." << std::endl;
});
callbackList.append( {
std::cout << "Got callback 2." << std::endl;
});
callbackList.append( {
std::cout << "Got callback 3." << std::endl;
});

// Now call forEach to remove the second callback
// The forEach callback prototype is void(const CallbackList::Handle & handle, const CallbackList::Callback & callback)
int index = 0;
callbackList.forEach([&callbackList, &index](const CL::Handle & handle, const CL::Callback & callback) {
std::cout << "forEach(Handle, Callback), invoked " << index << std::endl;
if(index == 1) {
callbackList.remove(handle);
std::cout << "forEach(Handle, Callback), removed second callback" << std::endl;
}
++index;
});

// The forEach callback prototype can also be void(const CallbackList::Handle & handle)
callbackList.forEach([&callbackList, &index](const CL::Handle & handle) {
std::cout << "forEach(Handle), invoked" << std::endl;
});

// The forEach callback prototype can also be void(const CallbackList::Callback & callback)
callbackList.forEach([&callbackList, &index](const CL::Callback & callback) {
std::cout << "forEach(Callback), invoked" << std::endl;
});

// Invoke the callback list
// The "Got callback 2" callback should not be triggered.
callbackList();
Errors may happen at // The forEach callback prototype can also be void(const CallbackList::Handle & handle)
callbackList.forEach([&callbackList, &index](const CL::Handle & handle) {
std::cout << "forEach(Handle), invoked" << std::endl;
});`

Thanks for the issue report.
What's the C++ compiler and which version you are using?
And seems you write your own main.cpp and try to compile it? Can you try to build the unittest in eventpp folder? Otherwise can you post the complete main.cpp?

Thanks for the issue report.
What's the C++ compiler and which version you are using?
And seems you write your own main.cpp and try to compile it? Can you try to build the unittest in eventpp folder? Otherwise can you post the complete main.cpp?

this is my main.cpp

#include <iostream>

#include <eventpp/callbacklist.h>

int main() {
    using CL = eventpp::CallbackList<void ()>;
    CL callbackList;

// Add some callbacks.
    callbackList.append([]() {
        std::cout << "Got callback 1." << std::endl;
    });
    callbackList.append([]() {
        std::cout << "Got callback 2." << std::endl;
    });
    callbackList.append([]() {
        std::cout << "Got callback 3." << std::endl;
    });

// Now call forEach to remove the second callback
// The forEach callback prototype is void(const CallbackList::Handle & handle, const CallbackList::Callback & callback)
    int index = 0;
    callbackList.forEach([&callbackList, &index](const CL::Handle & handle, const CL::Callback & callback) {
        std::cout << "forEach(Handle, Callback), invoked " << index << std::endl;
        if(index == 1) {
            callbackList.remove(handle);
            std::cout << "forEach(Handle, Callback), removed second callback" << std::endl;
        }
        ++index;
    });

// The forEach callback prototype can also be void(const CallbackList::Handle & handle)
    callbackList.forEach([&callbackList, &index](const CL::Handle & handle) {
        std::cout << "forEach(Handle), invoked" << std::endl;
    });

// The forEach callback prototype can also be void(const CallbackList::Callback & callback)
    callbackList.forEach([&callbackList, &index](const CL::Callback & callback) {
        std::cout << "forEach(Callback), invoked" << std::endl;
    });

// Invoke the callback list
// The "Got callback 2" callback should not be triggered.
    callbackList();

    return 0;
}

This is CMakeLists.txt

cmake_minimum_required(VERSION 3.17)
project(untitled1)

set(CMAKE_CXX_STANDARD 14)

include_directories(${PROJECT_SOURCE_DIR})

add_executable(untitled1 main.cpp)

I use clang 9.0.1 on centos8.

Error log is from my company's computer. The line number may be wrong.

I will investigate in clang. The library works well with GCC and MSVC.

The code compiles fine with clang 10.
Seems you are using code from the document, which is out of date and it uses a removed function.
So please only test the code under the folder tests/unittest, the document may be out of date :-).
I have updated the document.

Thanks a lot.