C++

'std::invoke' Is Not a Member of 'std' Fix

std::invoke lives in one header, <functional> - and on this library version almost nothing rescues it by accident: in the sweep probed here, covering all 106 standard headers in the library's header set, only <execution> pulls the name in on the side, and the textbook combination of <iostream> and <vector> does not. What this error does give, behind every failing header in the sweep, is a note that names the home header outright - a plainer path to the fix than most of its siblings offer. This guide pins down the one failing shape, the one transitive rescue, and the one include that fixes the error by specification.

Written December 2, 2026 · Tested with GCC 13.3.0 · 24 min read

By the end of this guide
  • The error: the include is missing, in a lean file
  • The textbook version fails too - with the same header-naming note
  • Neighboring headers: 104 fail the same way, and one rescues it
  • The fix

Written December 2, 2026

Tested with GCC 13.3.0

What this guide is

std::invoke is a function template defined in the header <functional>. It calls a Callable object with a set of arguments under one uniform rule: a plain function or function object is called with the arguments, a pointer to a member function is called on the object, pointer, or reference wrapper passed as the first argument after it, and a pointer to a member data member is read through that object, pointer, or wrapper instead. It has existed since C++17, it is constexpr since C++20, and its return type is std::invoke_result_t<F, Args...>. Since C++23 the same header also declares std::invoke_r, which converts the result to a return type the program names; the version sections below run both names.

Because the template lives in one specific header, 'invoke' is not a member of 'std' has one common cause:

  • The #include <functional> line is missing, and no other header in the file pulls the name in transitively. For this error, in the contexts probed here, GCC prints no suggestion tail on the error line and no help lines - but it does print one note line, behind every failing header in the sweep, and the note names the home header directly: 'std::invoke' is defined in header '<functional>'; did you forget to '#include <functional>'?, followed by a suggested include line. That makes this error easier to act on than most of its siblings in this family, whose notes never name the home header at all.

What makes this error worth separating from its relatives is the shape of the sweep behind it. The sweep probed here covers every one of the 106 standard headers in this library's header set, compiled as the only header in the program, and the outcome splits only two ways - the one uniform failure, or a rescue - and the rescue set has exactly two entries: <functional> itself and <execution>. The older siblings in this family - std::min_element among them - have their own guides and are linked at the end, not re-taught here.

The error: the include is missing, in a lean file

This program returns success if calling a function through std::invoke gives 42, but includes only <limits>:

C++
#include <limits>
int add_one(int x) { return x + 1; }
int main() {
    return std::invoke(add_one, 41) == 42 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:4:17: error: 'invoke' is not a member of 'std'
    4 |     return std::invoke(add_one, 41) == 42 ? 0 : 1;
      |                 ^~~~~~
a_limits.cpp:2:1: note: 'std::invoke' is defined in header '<functional>'; did you forget to '#include <functional>'?
    1 | #include <limits>
  +++ |+#include <functional>
    2 | int add_one(int x) { return x + 1; }

That is the whole output: one plain error line, then the note naming <functional> and the suggested include line it offers. No suggestion tail rides on the error line itself, and there are no help lines. The same program with no include at all fails the same way, with the same note:

C++
int add_one(int x) { return x + 1; }
int main() {
    return std::invoke(add_one, 41) == 42 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:17: error: 'invoke' is not a member of 'std'
    3 |     return std::invoke(add_one, 41) == 42 ? 0 : 1;
      |                 ^~~~~~
n_none.cpp:1:1: note: 'std::invoke' is defined in header '<functional>'; did you forget to '#include <functional>'?
  +++ |+#include <functional>
    1 | int add_one(int x) { return x + 1; }

The textbook version fails too - with the same header-naming note

The version most often written first - print the result of the call, with std::cout - gets the same error, without <functional>, and the same note:

C++
#include <iostream>
#include <vector>
int add_one(int x) { return x + 1; }
int main() {
    std::vector<int> v = {1, 2, 3};
    std::cout << std::invoke(add_one, 41) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:6:23: error: 'invoke' is not a member of 'std'
    6 |     std::cout << std::invoke(add_one, 41) << "\n";
      |                       ^~~~~~
t_textbook.cpp:3:1: note: 'std::invoke' is defined in header '<functional>'; did you forget to '#include <functional>'?
    2 | #include <vector>
  +++ |+#include <functional>
    3 | int add_one(int x) { return x + 1; }

What happened: on this library version, neither <vector> nor <iostream> pulls this name in transitively, so the call fails in a textbook file just as it does in a lean one. The note is still the whole story of the diagnostic: it names <functional>, and adding that one header is the fix, as the fix section shows. In the sweep probed here, the only header that rescues std::invoke besides <functional> itself is <execution>, and that rescue is a property of this library version, not a rule of the language. A program that uses std::invoke includes <functional>, whether or not it happens to compile without it.

Neighboring headers: 104 fail the same way, and one rescues it

In a sweep where each of the 106 standard headers in this library's header set was compiled as the only header in a program that returns success if calling a function through std::invoke gives 42, the outcome splits by compile result two ways. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 104 headers all fail with the plain is not a member of 'std' error line, no suggestion tail on that line, no help lines, and exactly one note line - the note naming <functional> quoted above, with its suggested include line:

  • <algorithm>
  • <any>
  • <array>
  • <atomic>
  • <barrier>
  • <bit>
  • <bitset>
  • <cassert>
  • <ccomplex>
  • <cctype>
  • <cerrno>
  • <cfenv>
  • <cfloat>
  • <charconv>
  • <chrono>
  • <cinttypes>
  • <ciso646>
  • <climits>
  • <clocale>
  • <cmath>
  • <codecvt>
  • <compare>
  • <complex>
  • <concepts>
  • <condition_variable>
  • <coroutine>
  • <csetjmp>
  • <csignal>
  • <cstdalign>
  • <cstdarg>
  • <cstdbool>
  • <cstddef>
  • <cstdint>
  • <cstdio>
  • <cstdlib>
  • <cstring>
  • <ctgmath>
  • <ctime>
  • <cuchar>
  • <cwchar>
  • <cwctype>
  • <deque>
  • <exception>
  • <expected>
  • <filesystem>
  • <format>
  • <forward_list>
  • <fstream>
  • <future>
  • <initializer_list>
  • <iomanip>
  • <ios>
  • <iosfwd>
  • <iostream>
  • <istream>
  • <iterator>
  • <latch>
  • <limits>
  • <list>
  • <locale>
  • <map>
  • <memory>
  • <memory_resource>
  • <mutex>
  • <new>
  • <numbers>
  • <numeric>
  • <optional>
  • <ostream>
  • <queue>
  • <random>
  • <ranges>
  • <ratio>
  • <regex>
  • <scoped_allocator>
  • <semaphore>
  • <set>
  • <shared_mutex>
  • <source_location>
  • <span>
  • <spanstream>
  • <sstream>
  • <stack>
  • <stacktrace>
  • <stdexcept>
  • <stdfloat>
  • <stop_token>
  • <streambuf>
  • <string>
  • <string_view>
  • <syncstream>
  • <system_error>
  • <thread>
  • <tuple>
  • <type_traits>
  • <typeindex>
  • <typeinfo>
  • <unordered_map>
  • <unordered_set>
  • <utility>
  • <valarray>
  • <variant>
  • <vector>
  • <version>

A file with no include at all fails the same way, with the same note. No failing sole header prints a suggestion tail or a help line in this sweep.

A sole <vector> file shows the failing shape in full:

C++
#include <vector>
int add_one(int x) { return x + 1; }
int main() {
    return std::invoke(add_one, 41) == 42 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 v_vector.cpp -o v_vector_prog
v_vector.cpp: In function 'int main()':
v_vector.cpp:4:17: error: 'invoke' is not a member of 'std'
    4 |     return std::invoke(add_one, 41) == 42 ? 0 : 1;
      |                 ^~~~~~
v_vector.cpp:2:1: note: 'std::invoke' is defined in header '<functional>'; did you forget to '#include <functional>'?
    1 | #include <vector>
  +++ |+#include <functional>
    2 | int add_one(int x) { return x + 1; }

And these headers rescue the program - it compiles and runs with no <functional> anywhere in the file:

  • <execution>
  • <functional>

Two contrasts with the neighboring sweeps are worth holding on to. First, there is no suggestion class and no link-failure shape in this sweep: every failing sole header fails at compile time with the same error and the same note, so the wrong-name suggestion tails that other names in this family carry - and the declared-but-not-defined link failure that std::swap produces behind five headers - have no counterpart for this name in the probed set. Second, the rescue set is the smallest in this family so far: where std::min_element is rescued transitively by five headers besides its home, std::invoke is rescued by one, <execution>. <functional> - the home header - is on the rescue list by definition, not by transitivity, and the other rescue is this library version's behavior, not a guarantee.

The fix

Add the header:

C++
#include <functional>
#include <iostream>
int add_one(int x) { return x + 1; }
int twice(int x) { return 2 * x; }
int main() {
    std::cout << std::invoke(add_one, 41) << " " << std::invoke(twice, 21) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
42 42

One include, <functional>, declares the template: the first call adds one to 41 and the second doubles 21, so the call reports 42 for two different functions instead of answering 42 for everything by accident.

The fixed programs: every callable kind, through one call shape

The point of std::invoke is that one call shape covers every kind of callable. This program runs them side by side - a free function, a member function called on an object and on a pointer to it, a member data pointer read through an object and through a pointer, a function object, and a lambda - and every one of the seven calls reports the value its own rule gives:

C++
#include <functional>
#include <iostream>
struct Box {
    int n;
    int add(int x) const { return n + x; }
};
struct Doubler {
    int operator()(int x) const { return 2 * x; }
};
int add_one(int x) { return x + 1; }
int main() {
    Box b{40};
    Box* bp = &b;
    std::cout << std::invoke(add_one, 41) << " "
              << std::invoke(&Box::add, b, 2) << " "
              << std::invoke(&Box::add, bp, 2) << " "
              << std::invoke(&Box::n, b) << " "
              << std::invoke(&Box::n, bp) << " "
              << std::invoke(Doubler(), 21) << " "
              << std::invoke([](int x) { return x - 2; }, 44) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 k_kinds.cpp -o k_kinds_prog
$ ./k_kinds_prog
42 42 42 40 40 42 42

Read in order: the free function adds one to 41 (42), the member function adds 2 to the stored 40 whether the object arrives by value (42) or by pointer (42), the member data pointer reads the stored value through the object (40) and through the pointer (40), the function object doubles 21 (42), and the lambda subtracts 2 from 44 (42).

A reference wrapper works as the object argument too: a member function called through std::ref and a member data value read through std::cref report 42 40 - the wrapper is unwrapped by the call, which is the same rule that lets a member pointer work on an object, a pointer, or a wrapper interchangeably:

C++
#include <functional>
#include <iostream>
struct Box {
    int n;
    int add(int x) const { return n + x; }
};
int main() {
    Box b{40};
    std::cout << std::invoke(&Box::add, std::ref(b), 2) << " " << std::invoke(&Box::n, std::cref(b)) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 k_ref.cpp -o k_ref_prog
$ ./k_ref_prog
42 40

The traits and the concept that describe this call live near it: std::is_invocable and std::is_invocable_r in <type_traits> (both since C++17), the C++20 concept std::invocable in <concepts>, and std::invoke_result_t, which names the call's return type. This program asserts all four over the same function and then prints the result through that named type:

C++
#include <concepts>
#include <functional>
#include <iostream>
#include <type_traits>
int add_one(int x) { return x + 1; }
int main() {
    static_assert(std::is_invocable_v<decltype(&add_one), int>);
    static_assert(std::is_invocable_r_v<int, decltype(&add_one), int>);
    static_assert(std::invocable<decltype(&add_one), int>);
    static_assert(std::same_as<std::invoke_result_t<decltype(&add_one), int>, int>);
    std::cout << std::invoke_result_t<decltype(&add_one), int>(std::invoke(add_one, 41)) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 k_traits.cpp -o k_traits_prog
$ ./k_traits_prog
42

And the call is usable in a constant expression - but only since C++20. std::invoke itself dates from C++17, and in C++17 it is not constexpr, so a static_assert over the call compiles under C++20 and fails under C++17 with a call to a non-constexpr function:

C++
#include <functional>
constexpr int add_one(int x) { return x + 1; }
int main() {
    static_assert(std::invoke(add_one, 41) == 42);
    return 0;
}
Command
$ g++ -std=c++20 c_constexpr.cpp -o c_constexpr_prog
$ ./c_constexpr_prog
Terminal
$ g++ -std=c++17 c_constexpr17.cpp -o c_constexpr17_prog
c_constexpr17.cpp: In function 'int main()':
c_constexpr17.cpp:4:44: error: non-constant condition for static assertion
    4 |     static_assert(std::invoke(add_one, 41) == 42);
      |                   ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
c_constexpr17.cpp:4:30: error: call to non-'constexpr' function 'std::invoke_result_t<_Callable, _Args ...> std::invoke(_Callable&&, _Args&& ...) [with _Callable = int (&)(int); _Args = {int}; invoke_result_t<_Callable, _Args ...> = int]'
    4 |     static_assert(std::invoke(add_one, 41) == 42);
      |                   ~~~~~~~~~~~^~~~~~~~~~~~~
In file included from c_constexpr17.cpp:1:
/usr/include/c++/13/functional:110:5: note: 'std::invoke_result_t<_Callable, _Args ...> std::invoke(_Callable&&, _Args&& ...) [with _Callable = int (&)(int); _Args = {int}; invoke_result_t<_Callable, _Args ...> = int]' declared here
  110 |     invoke(_Callable&& __fn, _Args&&... __args)
      |     ^~~~~~

Under C++14 the name is not there at all, even with <functional> included - the error is the same is not a member of 'std' sentence, with a different note, one that gives the version instead of the header:

Terminal
$ g++ -std=c++14 c_constexpr14.cpp -o c_constexpr14_prog
c_constexpr14.cpp: In function 'int main()':
c_constexpr14.cpp:4:24: error: 'invoke' is not a member of 'std'
    4 |     static_assert(std::invoke(add_one, 41) == 42);
      |                        ^~~~~~
c_constexpr14.cpp:4:24: note: 'std::invoke' is only available from C++17 onwards

The C++23 sibling: std::invoke_r

Since C++23 the same header declares std::invoke_r<R>, which calls the callable the same way and converts the result to the type R the program names. It runs under C++23 here, reporting 42:

C++
#include <functional>
#include <iostream>
int add_one(int x) { return x + 1; }
int main() {
    std::cout << std::invoke_r<long>(add_one, 41) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++23 r_invoke_r23.cpp -o r_invoke_r23_prog
$ ./r_invoke_r23_prog
42

Under C++20 the same program fails, and the failure is a version failure, not an include failure - <functional> is already in the file, and the note says so in its own way:

Terminal
$ g++ -std=c++20 r_invoke_r20.cpp -o r_invoke_r20_prog
r_invoke_r20.cpp: In function 'int main()':
r_invoke_r20.cpp:5:23: error: 'invoke_r' is not a member of 'std'
    5 |     std::cout << std::invoke_r<long>(add_one, 41) << "\n";
      |                       ^~~~~~~~
r_invoke_r20.cpp:5:23: note: 'std::invoke_r' is only available from C++23 onwards
r_invoke_r20.cpp:5:32: error: expected primary-expression before 'long'
    5 |     std::cout << std::invoke_r<long>(add_one, 41) << "\n";
      |                                ^~~~

So a missing-include error and a too-old-standard error can print the same first sentence for names in this header. The notes separate them: the include note names a header, the version note names a standard.

The neighboring names: two share the header, two live elsewhere

The names std::invoke is used alongside split by home header. std::bind - the function template in <functional> that binds arguments to a callable - shares this name's home, and in the lean context here - only <limits> included - it gets the same error shape, note included. It is shown here only as a neighbor, not re-taught:

C++
#include <limits>
int add_one(int x) { return x + 1; }
int main() {
    auto f = std::bind(add_one, 41);
    return f() == 42 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 b_bind_lean.cpp -o b_bind_lean_prog
b_bind_lean.cpp: In function 'int main()':
b_bind_lean.cpp:4:19: error: 'bind' is not a member of 'std'
    4 |     auto f = std::bind(add_one, 41);
      |                   ^~~~
b_bind_lean.cpp:2:1: note: 'std::bind' is defined in header '<functional>'; did you forget to '#include <functional>'?
    1 | #include <limits>
  +++ |+#include <functional>
    2 | int add_one(int x) { return x + 1; }

std::ref - the function template in <functional> that makes a std::reference_wrapper - fails the same first sentence in the same lean context, but with no note at all, which is a reminder that the helpful note is a per-name property, not a per-header one:

C++
#include <limits>
int main() {
    int x = 42;
    auto r = std::ref(x);
    return r.get() == 42 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 b_ref_lean.cpp -o b_ref_lean_prog
b_ref_lean.cpp: In function 'int main()':
b_ref_lean.cpp:4:19: error: 'ref' is not a member of 'std'
    4 |     auto r = std::ref(x);
      |                   ^~~

std::apply does not live in <functional> at all: it is a function template defined in <tuple> since C++17, which calls a callable with the elements of a tuple-like argument. Behind <limits> and <utility> - enough to build a std::pair, a tuple-like type - it fails with its own error and a note naming its own home, <tuple>:

C++
#include <limits>
#include <utility>
int add(int a, int b) { return a + b; }
int main() {
    return std::apply(add, std::pair<int, int>(20, 22)) == 42 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 b_apply_pair.cpp -o b_apply_pair_prog
b_apply_pair.cpp: In function 'int main()':
b_apply_pair.cpp:5:17: error: 'apply' is not a member of 'std'
    5 |     return std::apply(add, std::pair<int, int>(20, 22)) == 42 ? 0 : 1;
      |                 ^~~~~
b_apply_pair.cpp:3:1: note: 'std::apply' is defined in header '<tuple>'; did you forget to '#include <tuple>'?
    2 | #include <utility>
  +++ |+#include <tuple>
    3 | int add(int a, int b) { return a + b; }

And std::invoke_result_t, the type trait that names this call's return type, lives in <type_traits> - not in <functional>, even though the function it describes does. In the lean context it fails with its own error, a note naming <type_traits>, and a run of follow-on parse errors behind the first one:

C++
#include <limits>
int add_one(int x) { return x + 1; }
int main() {
    return std::invoke_result_t<decltype(&add_one), int>(42) == 42 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 b_invoke_result_lean.cpp -o b_invoke_result_lean_prog
b_invoke_result_lean.cpp: In function 'int main()':
b_invoke_result_lean.cpp:4:17: error: 'invoke_result_t' is not a member of 'std'
    4 |     return std::invoke_result_t<decltype(&add_one), int>(42) == 42 ? 0 : 1;
      |                 ^~~~~~~~~~~~~~~
b_invoke_result_lean.cpp:2:1: note: 'std::invoke_result_t' is defined in header '<type_traits>'; did you forget to '#include <type_traits>'?
    1 | #include <limits>
  +++ |+#include <type_traits>
    2 | int add_one(int x) { return x + 1; }
b_invoke_result_lean.cpp:4:33: error: expected primary-expression before 'decltype'
    4 |     return std::invoke_result_t<decltype(&add_one), int>(42) == 42 ? 0 : 1;
      |                                 ^~~~~~~~~~~~~~~~~~
b_invoke_result_lean.cpp:4:33: error: expected ';' before 'decltype'
    4 |     return std::invoke_result_t<decltype(&add_one), int>(42) == 42 ? 0 : 1;
      |                                 ^~~~~~~~~~~~~~~~~~
      |                                 ;
b_invoke_result_lean.cpp:4:51: error: expected unqualified-id before ',' token
    4 |     return std::invoke_result_t<decltype(&add_one), int>(42) == 42 ? 0 : 1;
      |                                                   ^
b_invoke_result_lean.cpp:4:53: error: expected unqualified-id before 'int'
    4 |     return std::invoke_result_t<decltype(&add_one), int>(42) == 42 ? 0 : 1;
      |                                                     ^~~

The neighboring names, fixed

Each neighboring name works under its own one added header, and std::invoke works over a standard function object too: this program reports 42 42 9 42 - std::apply adding the pair 20 and 22 from a tuple, the std::bind result adding one to 41, the value 9 written into x through the std::ref wrapper, and std::invoke calling a std::plus<int> object on 20 and 22:

C++
#include <functional>
#include <iostream>
#include <tuple>
int add(int a, int b) { return a + b; }
int add_one(int x) { return x + 1; }
int main() {
    int x = 7;
    auto bound = std::bind(add_one, 41);
    std::reference_wrapper<int> r = std::ref(x);
    r.get() = 9;
    std::cout << std::apply(add, std::make_tuple(20, 22)) << " " << bound() << " " << x << " " << std::invoke(std::plus<int>(), 20, 22) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 k_neighbors_fixed.cpp -o k_neighbors_fixed_prog
$ ./k_neighbors_fixed_prog
42 42 9 42

std::apply also works under its own home header alone, with no <functional> in the file: the tuple version of the lean program compiles and runs, returning success - the two names take different headers, and neither header substitutes for the other.

A different wording - and one case where the qualifier is not needed, and two where it is

Including <functional> is not the only half of the name. This program includes the header, drops the std:: qualifier, and passes a plain function - a pointer to a function belongs to no namespace, so no lookup finds the name:

C++
#include <functional>
int add_one(int x) { return x + 1; }
int main() {
    return invoke(add_one, 41) == 42 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 g_unqualified_ptr.cpp -o g_unqualified_ptr_prog
g_unqualified_ptr.cpp: In function 'int main()':
g_unqualified_ptr.cpp:4:12: error: 'invoke' was not declared in this scope; did you mean 'std::invoke'?
    4 |     return invoke(add_one, 41) == 42 ? 0 : 1;
      |            ^~~~~~
      |            std::invoke
In file included from g_unqualified_ptr.cpp:1:
/usr/include/c++/13/functional:110:5: note: 'std::invoke' declared here
  110 |     invoke(_Callable&& __fn, _Args&&... __args)
      |     ^~~~~~

That wording - was not declared in this scope, with did you mean 'std::invoke'? on the error line and a note pointing at the declaration in <functional> - is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::invoke; adding includes does not bring the name out of namespace std. A lambda as the callable fails the same way, for the same reason - a lambda's type is not in namespace std either:

C++
#include <functional>
#include <iostream>
int main() {
    std::cout << invoke([](int x) { return x + 1; }, 41) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 g_unqualified_lambda.cpp -o g_unqualified_lambda_prog
g_unqualified_lambda.cpp: In function 'int main()':
g_unqualified_lambda.cpp:4:18: error: 'invoke' was not declared in this scope; did you mean 'std::invoke'?
    4 |     std::cout << invoke([](int x) { return x + 1; }, 41) << "\n";
      |                  ^~~~~~
      |                  std::invoke
In file included from g_unqualified_lambda.cpp:1:
/usr/include/c++/13/functional:110:5: note: 'std::invoke' declared here
  110 |     invoke(_Callable&& __fn, _Args&&... __args)
      |     ^~~~~~

The same unqualified call with a std::plus<int> function object as its first argument behaves differently - it compiles and runs, printing 42:

C++
#include <functional>
#include <iostream>
int main() {
    std::cout << invoke(std::plus<int>(), 20, 22) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 g_unqualified_stdarg.cpp -o g_unqualified_stdarg_prog
$ ./g_unqualified_stdarg_prog
42

What happened: the function object belongs to namespace std, so argument-dependent lookup finds std::invoke from the arguments themselves, even without the qualifier. That is a property of the argument types, not a style to rely on: the same call over a plain function and over a lambda fails, as the transcripts above show. Write the qualifier either way.

The exact rule

  • std::invoke is declared by #include <functional>, and by no other header by rule. In the sweep probed here, every one of the other 104 headers in the library's header set - among them <iostream>, <vector>, <string>, <tuple>, <utility>, <algorithm>, and <version> - does not declare it, and neither does a file with no include at all.
  • 'invoke' is not a member of 'std' means the <functional> include is missing and nothing else in the file pulled the name in - or, under a standard before C++17, that the name does not exist yet in that standard. In the missing-include contexts probed here, GCC prints no suggestion tail on the error line and no help lines, and exactly one note line, and that note names <functional> with a suggested include line.
  • In the sweep probed here, the split is two-way: a sole header either fails with that one uniform shape, or rescues the program. No probed sole header produces a suggestion class or a declared-but-not-defined link-failure shape for this name - the contrast with std::min_element, whose sweep splits between a plain class and a wrong-name suggestion class and whose notes never name its home header.
  • In the sweep probed here, only one header besides <functional> rescues std::invoke transitively - <execution> - so a file whose only include is <execution> compiles and runs the call without <functional>. That is this library version's behavior, not a guarantee - in the probed set, that header is not required to pull std::invoke in.
  • The call handles every callable kind under one shape: a free function, a function object, and a lambda are called with the arguments; a pointer to a member function is called on the object, pointer, or reference wrapper passed after it; a pointer to a member data member is read through that object, pointer, or wrapper. The fixed programs here print 42 42 42 40 40 42 42 for those seven calls in order, and 42 40 for a member function and a member data value reached through std::ref and std::cref.
  • std::invoke is a C++17 function template and is constexpr since C++20: a static_assert over the call compiles under C++20, fails under C++17 with a call to a non-constexpr function, and under C++14 the name itself is absent with the note 'std::invoke' is only available from C++17 onwards. std::invoke_result_t<F, Args...> names its return type, std::is_invocable and std::is_invocable_r test the call, and the C++20 concept std::invocable states the same test as a constraint.
  • std::invoke_r is declared by the same #include <functional> since C++23: with the include it runs under C++23 and reports 42 here; under C++20 it gets 'invoke_r' is not a member of 'std' with the note that it is only available from C++23 onwards - a version failure in a file whose include is already right.
  • std::bind and std::ref are declared by the same #include <functional>: in the lean programs here, which include only <limits>, each gets its own is not a member of 'std' error when that include is missing - bind with the header-naming note, ref with no note at all.
  • std::apply is declared by #include <tuple> since C++17, and std::invoke_result_t is declared by #include <type_traits>: in the programs shown here each gets its own is not a member of 'std' error, with a note naming its own home header, when that include is missing - the trait's home is not the function's home.
  • 'invoke' was not declared in this scope for an unqualified invoke use over a plain function or a lambda is a different defect: qualify the name as std::invoke - the include alone does not bring the name out of namespace std. The same unqualified use with a std::plus<int> function object as its first argument compiles in the program probed here through argument-dependent lookup; qualify the name there too, so the call does not depend on its argument types.

The fixes, ranked

  • First, check the include list of the file that names std::invoke. If <functional> is not there, add it - that is the fix in the common case, and it holds whether the file failed in a lean context, failed in a textbook file, or happened to compile before through a transitive rescue.
  • Second, follow the note when one appears - for once, it is right. For this error, in the contexts probed here, the one note GCC prints names <functional> and offers the include line itself. Still take the fix from the name's home header if the note is ever absent: std::ref, in the same header, gets no note at all in the lean context probed here.
  • Third, do not reach for <tuple> because std::apply is in the same conversation. In the sweep probed here, <tuple> as the only include fails for std::invoke with the same error and the same note - and std::apply takes <tuple> for its own calls, as its lean failure shows. Each name takes its own header.
  • Fourth, do not reach for <type_traits> to fix the function call, and do not reach for <functional> to fix the trait. std::invoke_result_t lives in <type_traits>; in the sweep probed here <type_traits> as the only include fails for std::invoke too. A file that names both the function and the trait includes both headers.
  • Fifth, do not be reassured by <iostream>, <vector>, or <string> being present - in the sweep here none of them rescues this name at all - and do not remove <functional> because a file compiles without it: in the probed set only <execution> rescues it, and that rescue is not guaranteed.
  • Sixth, check the standard before changing the code when the include is already right. Under C++14 this name does not exist, and under C++17 it exists but is not constexpr, so a static_assert over the call needs C++20; std::invoke_r needs C++23. The note tells you which case you are in - it names a standard, not a header.
  • Seventh, if the error says was not declared in this scope instead, fix the name's qualifier to std::invoke as well - and do not rely on an unqualified call that happens to compile over a std function object, because the same call over a plain function and over a lambda fails.
  • Eighth, if the failing name is a neighboring one, check its header before fixing: bind and ref take the same added <functional>, while apply takes <tuple> and invoke_result takes <type_traits> - three headers for four names in one conversation.

Which fix for which cause

  • #include <functional> missing, std::invoke used in a file whose only include is one of the 104 headers listed as failing in the sweep section above, or that includes nothing: the message is 'invoke' is not a member of 'std', with no suggestion tail, no help lines, and one note naming <functional> - add #include <functional>.
  • std::invoke used in a textbook file including <iostream> and <vector>: the message is the same error with the same header-naming note - add #include <functional>.
  • std::invoke used with only <functional> or <execution> included: no error on this library version - add #include <functional> anyway when it is not the include already present, because the <execution> rescue is not guaranteed.
  • std::invoke used in a static_assert with <functional> included under C++17: the message is a call to a non-constexpr function - compile as C++20 or later, where the call is constexpr. Under C++14 the message is 'invoke' is not a member of 'std' with a version note - compile as C++17 or later for the call itself.
  • std::invoke_r used with <functional> included under C++20: the message is 'invoke_r' is not a member of 'std' with the note that it is only available from C++23 onwards - compile as C++23.
  • std::bind or std::ref used without <functional> in the lean context: the message names that name - bind with the header-naming note, ref with no note - add #include <functional>.
  • std::apply used without <tuple>: the message is 'apply' is not a member of 'std' with a note naming <tuple> - add #include <tuple>.
  • std::invoke_result_t used without <type_traits>: the message is 'invoke_result_t' is not a member of 'std' with a note naming <type_traits>, followed by parse errors - add #include <type_traits>.
  • invoke used without the std:: qualifier, <functional> included, over a plain function or a lambda: the message is 'invoke' was not declared in this scope; did you mean 'std::invoke'? - qualify the name as std::invoke. With a std::plus<int> function object as the first argument the same unqualified call compiles through argument-dependent lookup in the program probed here - qualify it anyway.

Checklist

  • Check the include list of the file that names the call, not just the project. The include must be in that file, or in a header that file includes directly by design.
  • Read the note before acting on the error sentence alone. For this error the note names the home header in the missing-include case and names a standard in the too-old-standard case - the two fixes are different, and the note is what separates them.
  • Do not import a neighboring name's fix into this error by conversation: in the probed set, <tuple> and <type_traits> both fail here too. Split neighboring names by home header first - invoke and bind and ref in <functional>, apply in <tuple>, invoke_result in <type_traits>.
  • Do not assume the textbook includes rescue this name, and do not delete a working <functional> line because a file compiles without it. In the sweep here <iostream> and <vector> do not rescue std::invoke at all, and the one header that does rescue it does so only transitively.
  • Match the standard to the use: the call itself needs C++17, a constant-expression call needs C++20, and std::invoke_r needs C++23 - all with <functional> included either way.
  • One header per name the file uses: std::vector comes from <vector> and std::string comes from <string>, even in a program whose subject is the call - and std::invoke comes from <functional> in return.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 42 42 for two free functions, and 42 42 42 40 40 42 42 for the seven callable kinds in order.
  • 'std::min_element' Is Not a Member of 'std' Fix - the same missing-include shape for the smallest-element search in <algorithm>: its sweep splits between a plain class and a wrong-name suggestion class where this guide's sweep is uniform, its notes never name its home header where this guide's note names <functional> outright, and its rescue set is five headers where this guide's is one.

Sources

  • cppreference, std::invoke, std::invoke_r — for std::invoke being a function template defined in <functional> since C++17, constexpr since C++20, that invokes a Callable with the given arguments under the uniform member-pointer and reference-wrapper rules, participating in overload resolution only when the call is invocable, returning std::invoke_result_t<F, Args...>; and for std::invoke_r being defined in the same header since C++23, converting the result to the named return type.
  • cppreference, Standard library header <functional> — for <functional> being the header that declares std::invoke and std::invoke_r (C++17 / C++23), std::bind (C++11), std::ref and std::cref (C++11), std::plus, and the std::reference_wrapper class template.
  • cppreference, std::apply — for std::apply being a function template defined in <tuple> since C++17 that calls a callable with the elements of a tuple-like argument.
  • cppreference, std::invoke_result — for std::invoke_result and std::invoke_result_t being type traits defined in <type_traits> since C++17 that name the return type of invoking a callable with a set of argument types.

Keep Exploring