我想做以下事情:
std::vector<int> a = {1,2,3}, b = {4,5,6}, c = {7,8,9};
for(auto&& i : join(a,b,c)) {
i += 1
std::cout << i; // -> 2345678910
}
Run Code Online (Sandbox Code Playgroud)
我试过用boost::range::join,这很好用:
auto r = boost::join(a,b);
for(auto&& i : boost::join(r,c)) {
i += 1;
std::cout << i; // -> 2345678910
}
Run Code Online (Sandbox Code Playgroud)
链接加入,阅读操作工作:
for(auto&& i : boost::join(boost::join(a,b),c))
std::cout << i; // -> 123456789
Run Code Online (Sandbox Code Playgroud)
但是,写作不起作用:
for(auto&& i : boost::join(boost::join(a,b),c)) {
i += 1; // Fails :(
std::cout << i;
} …Run Code Online (Sandbox Code Playgroud) 我有一个四/八树数据结构.我将一个单元格的子索引/ ptrs存储在一个数组中.数组中的每个位置表示子项相对于其父项的位置,例如2D:
// _____________
// | | |
// | 2 | 3 |
// |_____|_____|
// | | |
// | 0 | 1 |
// |_____|_____|
// for each cell, 4 children are always stored in row-major order
std::vector<std::array<Integer,4>> children;
Run Code Online (Sandbox Code Playgroud)
我知道子项的最大数量是Integer类型可以表示的值的子集.因此,我可以通过使用像' -1for ' Integer = int或std::numeric_limits<unsigned>::max()for for 这样的''magic''来识别一个单元格是否遗漏了一个孩子Integer = unsigned.这是std::optional<Integer>不能假设的.
据我所知,这种魔法价值的使用是其存在的理由之一std::optional.不过,我担心std::vector<std::optional<int>>内循环的表现.
所以,
表现会不会std::vector<std::optional<int>>比那更差std::vector<int>?(我已经在对"不存在的"价值进行比较).
或者,可以std::optional优化实施以提供与原始相同的性能int吗?如何?
std::optional在我的数据结构中混合我的函数的返回类型和魔术值听起来是一个非常糟糕的主意.我更喜欢保持一致,要么使用其中一个(至少在同一个上下文中).虽然我可以重载与幻数进行比较的函数: …
我正在从文件中读取对象的类型:
enum class type_index { ... };
type_index typeidx = read(file_handle, type_index{});
Run Code Online (Sandbox Code Playgroud)
根据类型索引,我想创建一个类型(在可能的类型列表中),并使用它做一些通用的(每种类型的相同通用代码):
std::tuple<type1, type2, ..., typeN> possible_types;
boost::fusion::for_each(possible_types, [&](auto i) {
if (i::typeidx != typeidx) { return; }
// do generic stuff with i
});
Run Code Online (Sandbox Code Playgroud)
那是:
这感觉就像switch具有运行时条件的语句,但是在编译时生成"案例".特别是,这根本不是一个for_each声明(我没有为vector,tuple,list中的所有元素做任何事情,而只是对单个元素做任何事情).
有更好的方式来表达/写出这个成语吗?(例如mpl::vector,std::tuple对于可能的类型使用an 而不是a ,使用与for_each算法不同的东西,...)
是否可以通过模板别名显式实例化模板类?
如果是这样,怎么样?否则,有人可以指向讨论和决定反对的ISO文件吗?
template<class T>
struct A { };
/// Explicit instantiate A for int:
template struct A<int>;
/// Alias
template<class T>
using B = A<T>;
/// Explicitly instantiate A for double via alias B:
template struct B<double>;
/// error: elaborated type refers to a non-tag type
Run Code Online (Sandbox Code Playgroud)
不应该实例化,A<double>因为B<T>它只是一个不同的名称A<T>?
在C++中为零大小的分配返回唯一地址的理由是什么?
背景:C11标准讲述了malloc(7.20.3内存管理功能):
如果请求的空间大小为零,则行为是实现定义的:返回空指针,或者行为就像大小是非零值一样,除了返回的指针不应用于访问对象.
也就是说,正如我所看到的那样,malloc总是成功进行零大小的分配,因为只有你可以使用零大小分配的指针才能调用其他一些内存分配函数free:
malloc返回NULL,free(NULL)可以,所以这可以被认为是成功的,NULL),唯一的条件是free该值也应该起作用.此外,C11(也是7.20.3)没有指定从malloc返回的地址必须是唯一的,只是它们必须指向不相交的内存区域:
如果分配成功,则返回的指针被适当地对齐,以便可以将其指定给指向任何类型对象的指针,然后用于在分配的空间中访问此类对象或此类对象的数组(直到空间被显式释放) .分配对象的生命周期从分配延伸到解除分配.每个这样的分配都应该产生一个指向与任何其他对象不相交的对象的指针.
零大小的所有对象都是不相交的AFAICT,这意味着malloc可以为多个零大小的分配返回相同的指针(例如,NULL会很好),或者每次返回不同的指针,或者为某些指针返回相同的指针等.
然后C++ 98带来了两个原始内存分配函数:
void* operator new(std::size_t size);
void* operator new(std::size_t size, std::align_val_t alignment);
Run Code Online (Sandbox Code Playgroud)
请注意,这些函数仅返回原始内存:它们不会创建或初始化任何类型的任何AFAICT对象.
你这样打电话给他们:
#include <iostream>
#include <new>
int main() {
void* ptr = operator new(std::size_t{0});
std::cout << ptr << std::endl;
operator delete(ptr, std::size_t{0});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
[new.delete.single]C++ 17标准的这一部分解释了它们,但是我看到它的关键保证在[basic.stc.dynamic.allocation]:
即使请求的空间大小为零,请求也可能失败.如果请求成功,则返回的值应为非空指针值(7.11)p0,与先前返回的值p1不同,除非该值p1随后传递给运算符delete.此外,对于21.6.2.1和21.6.2.2中的库分配函数,p0应表示与调用者可访问的任何其他对象的存储不相交的存储块的地址.通过作为零大小请求返回的指针的间接效果是不确定的.38
也就是说,他们必须始终返回成功的不同指针.这有点变化malloc.
我的问题是:这种变化背后的理由是什么?(也就是说,在C++中为零大小的分配返回唯一地址)
理想情况下,答案只是链接到论文(或其他一些来源),它们探索了替代方案并激发了他们的语义.通常我会针对这些C++ …
统一初始化是一个重要且有用的C++ 11特性.但是,您不能{}随处使用,因为:
std::vector<int> a(10, 0); // 10 elements of value zero
std::vector<int> b({10, 0}); // 2 elements of value 10 and 0 respectively
std::vector<int> c{10, 0}; // 2 elements of value 10 and 0 respectively
std::vector<int> d = {10, 0}; // 2 elements of value 10 and 0 respectively
auto e(0); // deduced type is int
auto f = 0; // deduced type is int
auto g{0}; // deduced type is std::initializer_list<int>
auto h = {0}; // …Run Code Online (Sandbox Code Playgroud) 是否可以同时提供对git子模块的https和ssh访问?
有些人可能更喜欢使用https而有些人可能只能使用ssh(例如因为他们在ssh隧道后面).
是否可以提供两个获取子模块的选项?
在C++标准(std::for_each)的§25.2.4.2中:
template<class InputIterator, class Function> Function
for_each(InputIterator first, InputIterator last, Function f);
Run Code Online (Sandbox Code Playgroud)
效果:将f应用于取消引用[first,last]范围内的每个迭代器的结果, 从第一个开始到最后一个 - 1.
optional从constexpr函数返回吗?我感兴趣的两个boost::optional和std::optional.他们的行为是否相同?
我想为零参数抑制GCC可变参数宏参数警告,例如通过以下方式生成:
// for illustration purposes only:
int foo(int i) { return 0; };
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
FOO(1);
^ warning: ISO C++11 requires at least one argument for the "..." in a variadic macro
Run Code Online (Sandbox Code Playgroud)
对于使用GCC 5.3.0时源文件中的特定宏定义.
在clang中,这样做如下:
// ... large file
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
#pragma clang diagnostic pop
// ... large file
// not necessary on the same file
FOO(1); // doesnt trigger the warning
Run Code Online (Sandbox Code Playgroud)
在gcc中,它看起来像是-pedantic一种神奇的警告类型,所以以下内容不起作用: …