如何在包扩展期间使用constexpr功能?

时间:2016-07-01 14:14:41

标签: c++ template-meta-programming

我想做以下事情:

// have a constexpr function
template<class T>
constexpr T square( T const i )
{
    return i * i;
}

// transform a std::integer_sequence<> by calling the constexpr function on every integer
template<class Fn, class T, T... values>
static constexpr auto make_type( Fn fn, std::integer_sequence<T, values...> )
{
    return std::integer_sequence<T, fn( values )...>{};
}

// so that I can use it like so
using type = decltype( make_type( square, std::integer_sequence<int, 1, 2, 3>{} ) );

但是,我收到以下错误:

  

... \ main.cpp | 19 |错误:&#39; fn&#39;不是常量表达式|

2 个答案:

答案 0 :(得分:5)

fn在常量表达式中不可用 - 它是一个标准的块范围变量。你必须将仿函数作为一种类型传递。

template <typename Fn, typename T, T... values>
static constexpr std::integer_sequence<T, Fn{}(values)...>
make_type(std::integer_sequence<T, values...>) {return {};}

并将您的功能重写为

struct Square {
    template <typename T> constexpr T operator()(T const& t)
    {return t*t;}
};

答案 1 :(得分:1)

除了constexpr不是函数指针类型的一部分之外,square是一个模板,因此你不能通过通常的隐式衰减形成指向它的指针。

但是没有必要改变make_type函数的签名来使其工作。将Square重写为仿函数:

struct Square {
    template<class T>
    constexpr T operator()( T const& i )
    {
        return i * i;
    }
};

并称之为:

using type = decltype( make_type( square{}, std::integer_sequence<int, 1, 2, 3>{} ) );

在C ++ 17中,您将能够使用constexpr lambda:

constexpr auto square = [](auto const& i) { return i * i; };

using type = decltype( make_type( square, std::integer_sequence<int, 1, 2, 3>{} ) );