标签: stdarray

constexpr std :: array with static_assert

#include <iostream>
#include <array>

int main(int argc, char **argv) {
    constexpr const std::array<int, 2> arr {{ 0, 1 }};
    constexpr const int arr2[] = { 0, 1};

    static_assert(arr[0] == arr2[0], "asdf");
    static_assert(arr[1] == arr2[1], "asdfasdf");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当编译gcc 4.8.24.9.1使用g++ test.cpp --std=c++11,编译成功.当编译clang 3.43.5利用clang++ test.cpp --std=c++11然而,编译失败:

test.cpp:8:16: error: static_assert expression is not an integral constant expression
        static_assert(arr[0] == arr2[0], "asdf");
                      ^~~~~~~~~~~~~~~~~
test.cpp:8:16: note: non-constexpr function 'operator[]' cannot be used …
Run Code Online (Sandbox Code Playgroud)

c++ static-assert constexpr c++11 stdarray

10
推荐指数
1
解决办法
1228
查看次数

在编译时将std :: array的每个元素相乘

我想将一个转换std::array为另一个std::array,将每个元素乘以一个特定的数字.

我现在拥有的东西显然不起作用:

#include <array>
#include <iostream>
#include <utility>

template <class T, size_t... Is, size_t N>
constexpr std::array<T, N> multiply(std::array<T, N> const &src,
                                  std::index_sequence<Is...>) {
    return std::array<T, N>{{src[Is]...}}; // How can I multiply each of src's elements?
}

int main(int argc, char *argv[]) {
    constexpr std::array<int, 3> arr = {1, 2, 3};
    constexpr auto t = multiply(arr, std::make_index_sequence<3>{});
    for (auto &el : t) std::cout << el << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何在编译时迭代每个元素,或者如何在编译时应用相同的函数(在我的情况下:乘以2)?

c++ template-meta-programming c++11 stdarray

10
推荐指数
1
解决办法
1258
查看次数

将char数组中的元素范围提取到字符串中

我想从char数组的开头提取一系列元素并将它们放入一个字符串中.范围可以小于或等于元素的数量.

这就是我想出的.

// buffer is a std::array<char, 128>

std::string message;

for (int i = 0; i < numberToExtract; ++i)
{
    message += buffer.at(i);
}
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?

我一直在寻找像std :: string的迭代器构造函数.例如, std::string(buffer.begin(), buffer.end())但我不希望所有的元素.

谢谢.

c++ arrays string c++11 stdarray

9
推荐指数
1
解决办法
6882
查看次数

调用隐式删除的默认构造函数

当我尝试编译我的C++项目时,我收到错误消息调用隐式删除的'std :: array'的默认构造函数.

头文件cubic_patch.hpp

#include <array>
class Point3D{
public:
    Point3D(float, float, float);
private:
    float x,y,z;
};

class CubicPatch{
public:
    CubicPatch(std::array<Point3D, 16>);
    std::array<CubicPatch*, 2> LeftRightSplit(float, float);
    std::array<Point3D, 16> cp;
    CubicPatch *up, *right, *down, *left;
};
Run Code Online (Sandbox Code Playgroud)

源文件cubic_patch.cpp

#include "cubic_patch.hpp"
Point3D::Point3D(float x, float y, float z){
    x = x;
    y = y;
    z = z;
}

CubicPatch::CubicPatch(std::array<Point3D, 16> CP){// **Call to implicitly-deleted default constructor of 'std::arraw<Point3D, 16>'**
    cp = CP;
}

std::array<CubicPatch*, 2> CubicPatch::LeftRightSplit(float tLeft, float tRight){
    std::array<CubicPatch*, 2> newpatch;
    /* …
Run Code Online (Sandbox Code Playgroud)

c++ stdarray

9
推荐指数
1
解决办法
2万
查看次数

有没有办法强制执行std :: array的完全初始化

我正在使用std::array<size_t, N>(N是一个固定的模板变量).

#include<array>
template<size_t N>
struct A{
   size_t function(std::array<size_t, N> arr){ return arr[N-1];} // just an example
};

int main(){
   A<5> a;
   a.function({{1,2,3,4,5}}));
}
Run Code Online (Sandbox Code Playgroud)

它工作正常.问题是这个其他代码是默默允许的:

   A.function({{1,2,3}}));
Run Code Online (Sandbox Code Playgroud)

也就是说,即使错过了元素,也会以array某种方式初始化,即使它被很好地定义(例如,剩余元素初始化为零,我不确定)这可能是错误的来源.

有没有办法强制执行额外元素的初始化?例如,通过生成编译器错误或警告.

我想到的一个选择是使用 initializer_list

   size_t function2(std::initializer_list<size_t> il){ assert(il.size() == N); ...}
Run Code Online (Sandbox Code Playgroud)

问题是,这最多会产生运行时错误并检查每次调用.我更喜欢编译器错误/警告.

我没有受到默认初始化的困扰,std::array<>{}而是由不完整的初始化所困扰.(也许没有什么可以做的,因为这是从T[N]静态数组的行为继承而来的.)

我试过用clang 3.5gcc 5.

arrays initialization c++11 stdarray

9
推荐指数
2
解决办法
482
查看次数

为什么不能用两层list-initializers初始化2D std :: array?

有人可以帮助我理解为什么我的编译器不能/不推断这个?(使用g ++ 7.3)

不起作用:

#include <array>
std::array<std::array<double,2>,2> f() {
 return {{0,0},{0,0}};
}
Run Code Online (Sandbox Code Playgroud)

工作良好:

#include <array>
std::array<std::array<double,2>,2> f() {
 return {std::array<double,2>{0,0},{0,0}};
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,这也失败了:

#include <array>
std::array<std::array<double,2>,2> f() {
 return std::array<std::array<double,2>,2>{{0,0},{0,0}};
}
Run Code Online (Sandbox Code Playgroud)

@ 1201ProgramAlarm指出添加另一组花括号有效:

#include <array>
std::array<std::array<double,2>,2> f() {
 return {{{0,0},{0,0}}};
}
Run Code Online (Sandbox Code Playgroud)

它使用聚合初始化,因为std::array没有用于brace-init-list的构造函数.那没关系,但那么为什么/如何运作?

std::array<double,2> x{1,2};
Run Code Online (Sandbox Code Playgroud)

为什么它处理这种情况但不处理嵌套的情况?

c++ list-initialization stdarray

9
推荐指数
1
解决办法
215
查看次数

函数调用中 std::array 中元素的确切数量

我有一个需要std::array给定大小的函数N

void func(std::array<int,3> x) {
  // do something
}

int main() {
  func({4,4,4}) // works
  func({4}) // works as well
}
Run Code Online (Sandbox Code Playgroud)

我理解为什么第二个调用也有效,我的问题是:有没有办法在编译时检查我实际传递了多少参数?

背景:我不想允许第二次调用,我希望用户准确传递N参数。

c++ metaprogramming stdarray

9
推荐指数
1
解决办法
178
查看次数

将std :: array转换为std :: vector

在下面的代码中,函数(foo)参数(std::vector)的大小可以是使函数成为通用函数的任何函数.但是,有时大小容器是已知的,因此可以使用std :: array.问题是转换std::arraystd::vector.解决这个问题的最佳方法是什么?std::vector在这种情况下,总是使用总是更好吗?

#include <iostream>
#include <array>
#include <vector>

using namespace std;

// generic function: size of the container can be anything
void foo (vector<int>& vec)
{
    // do something
}

int main()
{
   array<int,3> arr; // size is known. why use std::vector?
   foo (arr); // cannot convert std::array to std::vector

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ vector stdvector c++11 stdarray

8
推荐指数
1
解决办法
3934
查看次数

具有私有构造函数和自身静态数组的类

对不起,如果标题令人困惑,我找不到一个简单的方法来写一个简单的句子.无论如何,我面临的问题是:

 // header:
class SomeThing
{
 private:
   SomeThing() {} // <- so users of this class can't come up
                  //    with non-initialized instances, but
                  //    but the implementation can.

   int some_data; // <- a few bytes of memory, the default
                  //    constructor SomeThing() doesn't initialize it
 public:
   SomeThing(blablabla ctor arguments);

   static SomeThing getThatThing(blablabla arguments);

   static void generateLookupTables();
 private:

   // declarations of lookup tables
   static std::array<SomeThing, 64> lookup_table_0;
   static SomeThing lookup_table_1[64];
};
Run Code Online (Sandbox Code Playgroud)

getThatThing函数用于从查找表返回实例.

 // in the implementation file - …
Run Code Online (Sandbox Code Playgroud)

c++ initialization static-initialization c++11 stdarray

8
推荐指数
2
解决办法
1021
查看次数

如何static_assert std :: array成员的大小

我想明确关于成员变量的数组大小限制,以阻止其他人意外地进行愚蠢的更改.以下天真的尝试将无法编译:

struct Foo
{
    std::array< int, 1024 > some_array;
    static_assert( (some_array.size() % 256) == 0, "Size must be multiple of 256" );
    //^ (clang) error: invalid use of non-static data member 'some_array'
};
Run Code Online (Sandbox Code Playgroud)

即使std::array::sizeconstexpr,我也不能直接使用static_assert,因为函数和我的成员变量都不是静态的.

我想出的解决方案是使用decltype(因为我不想键入数组),如下所示:

static_assert( (decltype(some_array)().size() % 256) == 0, "Size must be multiple of 256" );
Run Code Online (Sandbox Code Playgroud)

这看起来像是默认构造一个右值,我认为它不是一个constexpr.

为什么这样做?

有没有更简洁的方法来实现静态断言?

c++ clang static-assert c++11 stdarray

8
推荐指数
2
解决办法
3387
查看次数