相关疑难解决方法(0)

可以使用C++ 14标准库的哪些部分以及哪些部分将成为constexpr?

使用新的轻松的C++ 14 constexpr规则,编译时编程变得更具表现力.我想知道标准图书馆是否也会升级以便利用.特别是std::initializer_list,std::pair,std::tuple,std::complex,std::bitsetstd::array看起来像总理候选人须注明constexpr批发.

问题:

  • 其标准库的部分现在被标记constexpr
  • 哪些其他部分可以标记constexpr
  • 例如,为什么不是来自<cmath><algorithm>标记的功能constexpr
  • 是否有向后兼容的原因不这样做?

c++ backwards-compatibility constexpr c++11 c++14

32
推荐指数
1
解决办法
1277
查看次数

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)

c++ constexpr c++14

5
推荐指数
1
解决办法
1237
查看次数

Variadic模板最大功能麻烦

我正在尝试编写一个可变参数模板,用于查找任意数量的数字的最大值(这仅用于练习可变参数模板).

但是,我有点碰壁,无法理解为什么我当前的尝试根本不起作用,并在编译时因错误而失败:

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)

c++ templates variadic-functions c++11

3
推荐指数
1
解决办法
2236
查看次数