How do I call invoke_result correctly for a member function? Or specifically for an operator member function. I tried std::invoke_result<T::operator[], size_type>
to no success. What would be the correct syntax in this case?
Don't. Use decltype(std::declval<T&>()[size_type{}])
or something similar (adjust the value category and the cv-qualification as needed).
invoke_result
is for when you have an invocable/callable object. You don't have one, so don't try to hammer square pegs into round holes.
decltype
takes an unevaluated operand. So no, it constructs nothing at runtime. –
Acicular What about as follows ?
std::invoke_result<decltype(&T::operator[]), T, size_type>
But this syntax should works with a single, and not template, operator[]
.
In case of template or overload, you should avoid std::invoke_result
and follows the decltype()
way suggested by T.C.
Or, maybe, you can wrap the call in a lambda function and apply std::invoke_result
to the lambda (if you really, really want to use std::invoke_result
).
Regarding the std::invoke_result
syntax, take in count that a pointer to a member function is a completely different things, compared to a pointer to a regular function. Anyway, you can roughly see it as a pointer to a regular function receiving an additional argument (in first position) corresponding to the object that call its method.
So, in your example, the first T
argument represent the object of type T
that call its operator.
cannot determine which instance of overloaded function ... is intended
. –
Pet operator[]
(const
and not-const
version?) in your class. –
Donell std::invoke_result
and member functions but I think it's a bad idea to use it to detect the returned type of a specific method. The T.C. solution is, IMHO, a better solution because works also in case of overloading and template methods. –
Donell const
version. The issue is caused by the presence of more than one operator[]
, so the compiler doesn't know which one choose when you write decltype(&T::operator[])
. –
Donell T
with const T
to see if it will work, but this didn't produce results. How does one go about resolving such ambiguities? I would assume that one can differentiate between the different overloads through the argument types given, however what can I do for the const
vs non-const
case. (I am asking out of curiosity - I do not plan to use this rather than T.C.'s version, but I assume there are cases where invoke_result
is a valid solution). –
Pet const
to the std::declval()
type: decltype(std::declval<T const &>()[size_type{}])
–
Donell invoke_result
version. It's not tragic though. –
Pet // Tank you! Thanks to your hint, I got the following
code:
struct C_STRUCT {
double Func(char, int&) { return 3.14; };
};
std::invoke_result<decltype(&C_STRUCT::Func), C_STRUCT,
char, int&>::type dbl_var_g = 3.14;
static_assert(std::is_same<decltype(dbl_var_g),
double>::value, "Несоответствие типов");
© 2022 - 2024 — McMap. All rights reserved.
T
here is a template typename anyways, so it should work in general for any class providing anoperator[]
with one argument ofsize_t
. – Pet