简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM