Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 nested std::conditional

Whats wrong with this expression?

template<class T, class FieldT>
using addRefU = typename std::conditional<
                            // ok when true. When false result has no reference at all
                            false,     
                            typename std::add_rvalue_reference< FieldT >::type,
                            typename std::conditional<
                                true,
                                typename std::add_rvalue_reference< FieldT >::type,
                                typename std::add_lvalue_reference< FieldT >::type
                            >
                        >::type;

int main()
{
        std::cout << std::is_rvalue_reference<
            addRefU<A, B>
        >::value << std::endl;
        std::cout << std::is_lvalue_reference<
            addRefU<A, B>
        >::value << std::endl;

}

http://coliru.stacked-crooked.com/a/21593805f2c6e634

As a result, it does not have reference at all. Are nested std::conditional's not allowed?

like image 821
tower120 Avatar asked Jun 24 '14 10:06

tower120


1 Answers

You forgot a ::type on the nested conditional:

template<class T, class FieldT>
using addRefU = typename std::conditional<
                            // ok when true. When false result has no reference at all
                            false,     
                            typename std::add_rvalue_reference< FieldT >::type,
                            typename std::conditional<
                                true,
                                typename std::add_rvalue_reference< FieldT >::type,
                                typename std::add_lvalue_reference< FieldT >::type
                            >::type
                        >::type;

int main()
{
        std::cout << std::is_rvalue_reference<
            addRefU<A, B>
        >::value << std::endl;
        std::cout << std::is_lvalue_reference<
            addRefU<A, B>
        >::value << std::endl;

}

Live on Coliru

like image 146
Angew is no longer proud of SO Avatar answered Sep 18 '22 19:09

Angew is no longer proud of SO