Thread: trailing return from std::tuple

  1. #1
    Registered User
    Join Date
    Sep 2015
    Location
    Italy
    Posts
    38

    trailing return from std::tuple

    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

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    "p" must be a compile-time constant. Thus this code is malformed.
    This code will compile:

    Code:
    #include <iostream>
    #include <tuple>
    
    using namespace std;
    
    class A
    {
    	tuple<string, int> data{ "abc", 1 };
    public:
    	template<size_t p> auto operator()() -> decltype(get<p>(data))
    	{
    		return get<p>(data);
    	}
    };
    
    int main()
    {
    	A a;
    	cout << a.operator()<0>() << endl;
    	cout << a.operator()<1>() << endl;
    
    	return EXIT_SUCCESS;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing std::tuple recursively
    By Dave11 in forum C++ Programming
    Replies: 5
    Last Post: 04-29-2015, 03:07 AM
  2. Boost tuple and Vector problem
    By LuckyPierre in forum C++ Programming
    Replies: 14
    Last Post: 09-29-2009, 01:26 AM
  3. boost tuple question
    By Elkvis in forum C++ Programming
    Replies: 1
    Last Post: 09-23-2009, 11:08 AM
  4. tuple usage
    By KIBO in forum C++ Programming
    Replies: 1
    Last Post: 06-24-2009, 02:49 AM
  5. trailing zero in Decimal
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-09-2008, 07:40 AM

Tags for this Thread