类型选择中的C++模板元编程问题

Rob*_*mey 5 boost c++11

以下代码说明了我的问题

#include <type_traits>
#include <limits>
#include <cstdint>
#include <boost/mpl/if.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/identity.hpp>

/////////////////////////////////////////////////////////////////
// safe_signed_range

template <
    std::intmax_t MIN,
    std::intmax_t MAX
>
struct safe_signed_range {
};

/////////////////////////////////////////////////////////////////
// safe_unsigned_range

template <
    std::uintmax_t MIN,
    std::uintmax_t MAX
>
struct safe_unsigned_range {
};

template<class T, class U>
using calculate_max_t = typename boost::mpl::if_c<
    std::numeric_limits<T>::is_signed
    || std::numeric_limits<U>::is_signed,
    std::intmax_t,
    std::uintmax_t
>::type;

template<typename T, typename U>
struct test {

    typedef calculate_max_t<T, U> max_t;
    static_assert(std::is_same<max_t, std::intmax_t>::value, "unexpected value for max_t");
    static_assert(std::is_signed<max_t>::value, "check parameter");

/*
    typedef typename boost::mpl::if_c<
        std::is_signed<max_t>::value,
        safe_signed_range<std::numeric_limits<max_t>::min(), std::numeric_limits<max_t>::max()>,
        safe_unsigned_range<std::numeric_limits<max_t>::min(), std::numeric_limits<max_t>::max()>
    >::type type;
*/

    typedef typename boost::mpl::eval_if_c<
        std::is_signed<max_t>::value,
        boost::mpl::identity<safe_signed_range<std::numeric_limits<max_t>::min(), std::numeric_limits<max_t>::max()> >,
        // error shows up here
boost::mpl::identity<safe_unsigned_range<std::numeric_limits<max_t>::min(), std::numeric_limits<max_t>::max()> >
    >::type type;
};

test<int, int> t1;
//test<int, unsigned> t2;
//test<unsigned, int> t3;
//test<unsigned, unsigned> t4;

int main(){
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误出现在Clang编译器中

/Users/robertramey/WorkingProjects/safe_numerics/test/test_z.cpp:116:50: Non-type template argument evaluates to -9223372036854775808, which cannot be narrowed to type 'std::uintmax_t' (aka 'unsigned long')

这看起来像Boost Mpl没有选择正确的类型.首先我怀疑(并且实际上仍然怀疑)if的所有参数都在扩展,所以我改为使用eval_if,但我仍然遇到问题.我包含了static_assert以检查参数,并且可以通过最简单的测试使其失败 - 尽管它在所有组合上都失败了.如果有人能向我解释我的错误,我将不胜感激.

Rap*_*ptz 6

问题是您实例化的模板(无论如何boost::mpl::identity)对当前类型的模板无效safe_unsigned_range.解决方案是根据传递给的布尔谓词推迟模板实例化boost::mpl::eval_if_c.

为此,我们必须首先identity为两种范围类型编写自己的版本:

template<typename Integer, Integer Top, Integer Bottom>
struct defer_unsigned_lazily {
    using type = safe_unsigned_range<Top, Bottom>;
};

template<typename Integer, Integer Top, Integer Bottom>
struct defer_signed_lazily {
    using type = safe_signed_range<Top, Bottom>;
};
Run Code Online (Sandbox Code Playgroud)

它的工作方式是实例化不会发生,直到我们typename X::type基本上给我们懒惰的语义boost::mpl::identity.

然后我们像这样更改typedef:

using limits = std::numeric_limits<max_t>;
typedef typename boost::mpl::eval_if_c<
    std::is_signed<max_t>::value, 
    defer_signed_lazily<max_t, limits::min(), limits::max()>, 
    defer_unsigned_lazily<max_t, limits::min(), limits::max()>
>::type type;
Run Code Online (Sandbox Code Playgroud)

之后,它应该按照预期在Clang和GCC上进行编译.

演示