gpt4 book ai didi

C++11 嵌套 std::conditional

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:36:28 27 4
gpt4 key购买 nike

这个表达式有什么问题吗?

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?

最佳答案

您在嵌套的 conditional 上忘记了 ::type:

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

关于C++11 嵌套 std::conditional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24383740/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com