Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template argument not deduced for inner class

I want to do something like this:

#include <iostream>

template<typename T>
class myclass
{
    T something;
public:
    myclass(T something) : something{ something }
    { }

    struct result
    {
        T value;
    };

    result get_result()
    {
        return{ something };
    }
};

template<typename T>
std::ostream& operator<<(std::ostream& stream, const typename myclass<T>::result& res)
{
    return stream << res.value;
}

int main()
{
    myclass<int> m(0);
    std::cout << m.get_result() << "\n";
}

In this case neither gcc nor msvc find the overloaded stream operator when I put in a result (which is dependent on a templated outer class). Is what I am trying to do even possible?

like image 951
kamulos Avatar asked Feb 26 '16 09:02

kamulos


1 Answers

T is non-deductible for myclass<T>::result.

You may define operator<< inside the class to solve your issue:

struct result
{
    T value;


    friend std::ostream& operator<<(std::ostream& stream, const result& res)
    {
        return stream << res.value;
    }
};

Demo

like image 195
Jarod42 Avatar answered Sep 30 '22 01:09

Jarod42