Hi,
I'm on C++11, is it possible from decltype to determine which type the call operator must use as return type from std::tuple? The testing code:
Code:
#include <iostream>
#include <tuple>
using namespace std;
class A
{
tuple<string, int> data{"abc", 1};
public:
auto operator()(size_t p) -> decltype(get<p>(data))
{
return get<p>(data);
}
};
int main()
{
A a;
cout << a(0) << endl;
cout << a(1) << endl;
return EXIT_SUCCESS;
}
It doesn't compile and returns:
Code:
test.cpp:10:44: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘size_t {aka long unsigned int}’ to binary ‘operator<’
auto operator()(size_t p) -> decltype(get<p>(data))
^
test.cpp: In function ‘int main()’:
test.cpp:19:13: error: no match for call to ‘(A) (int)’
cout << a(0) << endl;
^
test.cpp:20:13: error: no match for call to ‘(A) (int)’
cout << a(1) << endl;
^
But if I use get<0>(data) instead:
Code:
#include <iostream>
#include <tuple>
using namespace std;
class A
{
tuple<string, int> data{"abc", 1};
public:
auto operator()(size_t p) -> decltype(get<0>(data))
{ // ^ - - - - - - size_t literal
return get<0>(data);
} // ^ - - - - - - size_t literal
};
int main()
{
A a;
cout << a(0) << endl;
cout << a(1) << endl;
return EXIT_SUCCESS;
}
It compiles and returns:
Code:
test.cpp:10:25: warning: unused parameter ‘p’ [-Wunused-parameter]
auto operator()(size_t p) -> decltype(get<0>(data))
^
abc
abc
Is there a way to have that code running using the size_t p argument?
Thanks in advance for any answer, best regards
Franco