使用新的轻松的C++ 14 constexpr规则,编译时编程变得更具表现力.我想知道标准图书馆是否也会升级以便利用.特别是std::initializer_list,std::pair,std::tuple,std::complex,std::bitset和std::array看起来像总理候选人须注明constexpr批发.
问题:
constexpr?constexpr?<cmath>和<algorithm>标记的功能constexpr?在回答完这个问题并阅读本文后,看看这段代码,我想constexpr find用简单的数组类实现.
考虑以下示例:
#include <cstddef>
template <class It, class T>
constexpr auto constexpr_find(const It& b, const It& e, T value) {
auto begin = b;
while (begin != e) {
if (*begin == value) break;
++begin;
}
return *begin;
}
template<typename T, size_t N>
class array
{
public:
typedef T* iterator;
typedef const T* const_iterator;
constexpr auto begin() const { return const_iterator(array_); }
constexpr auto end() const { return const_iterator(array_ + N); …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个可变参数模板,用于查找任意数量的数字的最大值(这仅用于练习可变参数模板).
但是,我有点碰壁,无法理解为什么我当前的尝试根本不起作用,并在编译时因错误而失败:
prog.cpp: In function 'A myMax(A, A, Args ...) [with A = int, Args = {}]':
prog.cpp:7:35: instantiated from 'A myMax(A, A, Args ...) [with A = int, Args = {int}]'
prog.cpp:22:26: instantiated from here
prog.cpp:7:35: error: no matching function for call to 'myMax(int)'
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
#include <iostream>
template <typename A, typename ... Args>
A myMax(A a, A b, Args ... args)
{
return myMax(myMax(a,b),args...);
}
template <typename A>
A myMax(A a,A b)
{
if (a>b)
return a;
else
return …Run Code Online (Sandbox Code Playgroud)