C ++ 11嵌套std :: conditional

时间:2014-06-24 10:02:50

标签: c++ c++11 typetraits

这个表达方法有什么问题?

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

因此,它根本没有参考。嵌套的std :: conditional&#39; s是不允许的?

1 个答案:

答案 0 :(得分:6)

您忘记了嵌套::type上的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