0

If I store a tuple in a class as such:

class BaseA { } //So that I can store A in a class

template <typename Args...>
class A : public BaseA {
public:
    //I'm omitting the constructors
private:
    std::tuple<Args...> storedTup;
}

Would you be able to retrieve the values later on by doing something along the lines of this?

//Change BaseA
class BaseA {
public:
    virtual ~BaseA(){}
    auto returnTuple();
}

//Change A
template <typename Args...>
class A : public BaseA {
public:
    auto returnTuple() -> decltype(storedTup) {
        return storedTup;
    }
private:
    std::tuple<Args...> storedTup;
}

I understand that this doesn't work but is there an easy fix that I am overlooking. From what I have seen, decltype can use members passed through the function (In my case returnTuple) but since my tuple is already stored that won't really help. Would there be another way to make the auto return type that of the tuple?

TrevorPeyton
  • 629
  • 3
  • 10
  • 22
  • 2
    What is `BaseA::returnTuple()`? – Barry Jan 21 '15 at 00:36
  • @Barry You can't save a template variable so you have class A save as BaseA. This may help what I'm saying. http://stackoverflow.com/questions/569073/c-stdmap-of-template-class-values – TrevorPeyton Jan 21 '15 at 00:42

1 Answers1

3

Since returnTuple() needs to have a uniform return type, no, you can't do this. You'd need to come up with some way to express the tuple in a uniform way, perhaps by a polymorphic type, or a vector of discriminated unions, etc.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Even with auto in C++14? I thought I read that that's something they added. I'll find an article, hang on. http://en.wikipedia.org/wiki/C%2B%2B14#Function_return_type_deduction – TrevorPeyton Jan 21 '15 at 00:54
  • @TrevorPeyton: Yes you can deduce the return type. But it must be a single type. What type would `auto` resolve to in `BaseA::returnTuple()`? – John Zwinck Jan 21 '15 at 01:11
  • std::tuple. If you knew the args it would be possible to return: std::tuple. That's what auto would do especially if I could define it with decltype. --- Sorry, that is supposed to be override by A. – TrevorPeyton Jan 21 '15 at 01:24
  • But `BaseA` doesn't have or know about `Args...`. So you've got a contradiction, an impossibility. – John Zwinck Jan 21 '15 at 01:25
  • Well if auto means that the compiler finds the type I figured that the compiler could derive the type. It was just a guess though. – TrevorPeyton Jan 21 '15 at 01:29
  • Yes, it needs to find **the** type. What is **the**, **singular**, never-changing return type in the base class? Hint: there is none, it's not known except by the derived class. In other words, C++11 `auto` is not `boost::any`. – John Zwinck Jan 21 '15 at 01:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69265/discussion-between-trevorpeyton-and-john-zwinck). – TrevorPeyton Jan 21 '15 at 01:36