我希望在 C++20 中执行以下操作:
template <class... Ts>
struct overloaded : Ts... {
using Ts::operator( )...;
};
// Updated to work with C++17
#if (_cplusplus != 202002L) // check for C++17 or C++20
// Deduction guide, google `CTAD for aggregates` for more info
template <typename... Ts>
overloaded(Ts...) -> overloaded<Ts...>; // not needed before C++20
#endif
template <typename T, long int C = 0>
void emit(T const& data) {
auto emit = overloaded {
[&](const auto& value) {
mOs << value;
},
[&](const …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用clang 11.0.1、c++14为 PS4 平台编译此代码。初始化some_array为std::arraystd::array<char, 48> some_array{{}};
auto* const characters = std::remove(some_array.begin(),
some_array.begin() + length, filtered_character);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
'auto *const' has incompatible initializer of type std::_Array_iterator<char, 48>
Run Code Online (Sandbox Code Playgroud)
我不确定是否需要使用cmake或与逻辑有关的内容来抑制警告。
我的构造函数需要一个std::array.我试图给它一个默认值,但调用没有参数的构造函数会出现此错误:
$ g++ -std=c++11 -Wall -Werror -Wextra -pedantic-errors test.cpp Position.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:14:29: error: array must be initialized with a brace-enclosed initializer
Position *y = new Position();
^
test.cpp:14:29: error: too many initializers for ‘std::array<unsigned char, 8ul>’
Run Code Online (Sandbox Code Playgroud)
行号不同.这是我的代码:
// test.cpp
int main() {
Position *x = new Position({1,1,1,1,1,1,1,1}); // works
Position *y = new Position(); // does not work
}
// Position.cpp
#include <cstdint>
#include <array>
#include "Position.h"
Position::Position( std::array<uint8_t,8> columns_ ) { …Run Code Online (Sandbox Code Playgroud) 我想要一个有数组元素作为成员的元组.特别是,我希望这个数组元素是2D - 所以任何答案都必须超过1D.我理想的是我可以用初始化列表初始化的东西,例如std::tuple<ARRAY_TYPE, ...>({{0, 1}, {2, 3}}, ...).
看起来这样的元组很难构造,需要手动初始化(即,用于循环等).这是我尝试过的:
std::tuple<int[M][N], ...> - 由于C风格数组的限制,这不起作用.元组本身是一个有效的类型,但初始化需要手动完成(不是在构建时).
std::tuple<std::array<std::array<int, M>, N>, ...>- 我认为这会起作用,但由于某种原因,像std::tuple<std::array<std::array<int, 2>, 2>, ...>({{0, 1}, {2, 3}}, ...)"没有匹配的构造函数错误"的失败.它确实在1D工作.
std::tuple<std::vector<std::vector<int>>, ...>({{0, 1}, {2, 3}}, ...)实际上确实有效,但是这里的矢量似乎有些过分
有什么想法吗?有什么方法可以让C风格的数组工作吗?那将是理想的.
我想在std :: array对象上使用按位数据转换,为此,我需要知道存储数组地址是否安全,或者是否存在更改数据位置的函数.例如:
std::array<int, 100> array;
int* startMarker = array.data();
(filing the array and doing operations on it)
std::cout << *startMarker << std::endl;
Run Code Online (Sandbox Code Playgroud)
谢谢你的回答.
尝试在googletest中使用std :: array时出现此错误.以下是产生此错误的最小示例:
arr.cpp
#include "gtest/gtest.h"
#include <array>
TEST(Test, Positive) {
EXPECT_NO_THROW({
const std::array<unsigned char, 16> foo = {1, 2, 3};
});
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Run Code Online (Sandbox Code Playgroud)
我使用了来自github 的当前googletest代码.制作和安装googletest.
作为编译器,我在Ubuntu 14.04 LTS上使用了clang3.8.
使用follownig命令:
clang++ -std=c++11 -o arr arr.cpp
Run Code Online (Sandbox Code Playgroud)
结果是:
arr.cpp:6:41: error: too many arguments provided to function-like macro invocation
const std::array<unsigned char, 16> blasdasd = {1, 2, 3};
^
/usr/local/include/gtest/gtest.h:1845:9: note: macro 'EXPECT_NO_THROW' defined here
#define EXPECT_NO_THROW(statement) \
^
arr.cpp:5:5: note: …Run Code Online (Sandbox Code Playgroud) 我正在用C++实现一个Matrix类,我希望能够用一个初始化列表来初始化它,如下所示:
Matrix<double, 2, 3> m({{1,2,1},
{0,1,1}});
Run Code Online (Sandbox Code Playgroud)
在类中我实现了这样的构造函数:
template<typename Ty, unsigned int rows, unsigned int cols>
class Matrix
{
std::array<std::array<Ty, cols>, rows> data;
public:
Matrix(const std::initializer_list<std::initializer_list<Ty>>& list)
{
int i = 0;
for (auto& list_row : list)
{
int j = 0;
for (auto& list_value : list_row)
{
data[i][j++] = list_value;
}
i++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码工作,但我有几个问题.是否可以使它安全,所以它只需要一定大小的初始化列表(矩阵的大小),其次我可以使代码更简洁 - 我曾想象过这样的事情:
Matrix(const std::initializer_list<std::initializer_list<Ty>>& list)
: data(list)
{ }
Run Code Online (Sandbox Code Playgroud)
它不起作用,但任何更好的方法,将不胜感激.
是否有相应的memset std::array?我认为memset在将数组初始化为零而不是循环遍历数组时应该表现得更好.我搜索了一个,std::array但在网上找不到任何东西.
关于的帮助班std::tuple_size<std::array>,我有两个问题std::array。
首先,类中有一个constexpr size()成员函数std::array,为什么需要一个std::tuple_size<std::array>?
第二,这个名字是否tuple_size令人误解?
我有一个性能关键代码,可以查询N个索引。如何static_assert在不牺牲性能的情况下检查是否给出了准确的N个索引?
#include <array>
template<int N>
void test(const std::array<int, N>& indices)
{
// static_assert: has three elements.
return;
}
int main()
{
test<3>({1, 2, 3}); // OK
test<3>({1, 2}); // Needs to crash, because 2 < 3
test<2>({1, 2, 3}); // Crashes, because 3 > 2
test<2>({1, 2}); // OK
return 0;
}
Run Code Online (Sandbox Code Playgroud) c++ ×10
stdarray ×10
c++11 ×5
stl ×2
algorithm ×1
arrays ×1
c++20 ×1
constructor ×1
dereference ×1
googletest ×1
iterator ×1
lambda ×1
memset ×1
pointers ×1
stdtuple ×1