Jun 11, 2015

[c++] extract inner type

#include 
using namespace std;

class Fun
{
    struct InnerType
    {
        int i{42};
    };

public:
    auto run()
    {
        return InnerType{};
    }

    auto run2() -> InnerType;
};

template< typename R, typename _>
void extractInnerType( R(_::*)() )
{
    cout << R{}.i << endl;
}

template< typename T>
T innerType{};

int main(void)
{
    // Blog's question:
    Fun f;
    decltype(f.run()) d{};
    cout << d.i << endl;

    //variable template
    cout << innerType<decltype(f.run())>.i << endl;

    //--- c++98 style
    extractInnerType(true ? nullptr : &Fun::run2);
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.