Question Details

No question body available.

Tags

c++ std-ranges stdtuple

Answers (2)

Accepted Answer Available
Accepted Answer
February 17, 2026 Score: 7 Rep: 33,888 Quality: Expert Completeness: 80%

[...] Is there such a thing in std?

No, there is no such function object in the standard library. std::get is a function template, not a callable object, so it can't be passed directly as a projection. There has been interest in making std::get usable as a function object (see P2769 — they called "std::get_element"), but nothing has been standardized yet.


That being said, your getitem implementation is a perfectly reasonable solution. I'd just add const and noexcept to make it behave more like a standard function object:

template 
struct getitem {
    template 
    constexpr decltype(auto) operator()(T&& x) const noexcept {
        return std::get(std::forward(x));
    }
};

In the meantime, if you don't want a custom struct, a plain lambda is the idiomatic workaround.

February 17, 2026 Score: 1 Rep: 4,237 Quality: Low Completeness: 80%

For the record, what you must not do: if you want to use std::get directly (thought, in the end, it is more verbose than the lambda version as you need to repeat the template arguments):

#include 
#include 
#include 
#include 
int main() {
    using tuplet = std::tuple;
    std::vector v = {
        {'a', 2, 1.0}, {'c', 1, 3.0}, {'b', 4, 2.0}, {'d', 3, 4.0}};
    [[maybeunused]] auto m =
        std::ranges::max(v, {},
                         staticcast(
                             &std::get));
}

LIVE

So far compilers accept it: you must pass explicitly the tuple arguments, get explicitly the function address and cast it to the suitable function pointer type (I honestly don't know why I couldn't pass std::get directly) but it's UB as function from standard library are not addressable except if mentioned otherwise: https://eel.is/c++draft/namespace.std#6.

As a consequence, there cannot be a std::getitem except if the standard committee decided to make it addressable.

The only solution is then to make your own, in the line of this other answer from JeJo.

Or this debatable version:

#include 
#include 
#include 
#include 

using tuplet = std::tuple;

template constexpr decltype(auto) getitem(std::tuple const& x) noexcept { return std::get(x); }

int main() { std::vector v = { {'a', 2, 1.0}, {'c', 1, 3.0}, {'b', 4, 2.0}, {'d', 3, 4.0}}; [[maybe_unused]] auto m = std::ranges::max(v, {}, getitem); }

LIVE