Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(exercise): replace std::result_of with std::invoke_result since the old one is deprecated in C++20; #273

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Q-Wednesday
Copy link

@Q-Wednesday Q-Wednesday commented Oct 30, 2023

Description

refer to https://en.cppreference.com/w/cpp/types/result_of , std::result_of was deprecated since C++ 20

When I build the exercise code with following clang++:

─╯
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin23.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

I got error:

clang++ main.cpp -o main.out -std=c++2a -pedantic
In file included from main.cpp:19:
./thread_pool.hpp:99:39: error: no type named 'result_of' in namespace 'std'
    using return_type = typename std::result_of<F(Args...)>::type;
                        ~~~~~~~~~~~~~~^~~~~~~~~
./thread_pool.hpp:99:48: error: expected ';' after alias declaration
    using return_type = typename std::result_of<F(Args...)>::type;
                                               ^
                                               ;
./thread_pool.hpp:102:53: error: use of undeclared identifier 'return_type'
    auto task = std::make_shared<std::packaged_task<return_type()>>(
                                                    ^
./thread_pool.hpp:102:53: error: template argument for template type parameter must be a type
    auto task = std::make_shared<std::packaged_task<return_type()>>(
                                                    ^~~~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/future:1558:20: note: template parameter is declared here
    template <class> friend class packaged_task;
                   ^
In file included from main.cpp:19:
./thread_pool.hpp:106:17: error: use of undeclared identifier 'return_type'
    std::future<return_type> res = task->get_future();
                ^
5 errors generated.
make: *** [main.out] Error 1

Change List

  • replace the deprecated type in exercises/7/7.1

Reference

std::result_of

@@ -96,7 +97,7 @@ inline ThreadPool::ThreadPool(size_t threads): stop(false) {
template<class F, class... Args>
decltype(auto) ThreadPool::enqueue(F&& f, Args&&... args) {
// deduce return type
using return_type = typename std::result_of<F(Args...)>::type;
using return_type = typename std::invoke_result<F,Args...>::type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using return_type = typename std::invoke_result<F,Args...>::type;
using return_type = std::invoke_result_t<F,Args...>;

Copy link
Owner

@changkun changkun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the relevant book content should also be revised as well.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants