我正在审查C++ - 17 std::optional类模板的接口,并注意到reset和assignment来自nullopt未标记为constexpr.
这是疏忽还是有理由说这个操作不能标记为constexpr?
我尝试使用Ranges-V3库将一个值容器切割成一系列范围,使相邻范围共享边界元素.
考虑以下:
using namespace ranges;
std::vector<int> v = { 1, 2, 3, 0, 4, 0, 5, 0, 6, 7, 8, 0, 0, 9 };
auto myRanges = v | /* something like adjacent split */
for_each( myRanges, []( auto&& range ){ std::cout << range << std::endl;} );
Run Code Online (Sandbox Code Playgroud)
我想根据区域是否满足两个标准,将范围划分为重叠的子范围:
期望的输出:
[1,2,3]
[3,0,4,0,5,0,6]
[6,7,8]
[8,0,0,9]
Run Code Online (Sandbox Code Playgroud)
我的尝试:
auto degenerate =
[]( auto&& arg ){
return distance( arg ) < 2;
};
auto myRanges = v | view::split(0) | view::remove_if( degenerate ); …Run Code Online (Sandbox Code Playgroud) 我最近在clang和gcc之间遇到了关于void_t属性检测和受保护/私有类信息的一些不同行为.考虑一个类型特征,定义如下:
#include <type_traits>
template<typename T, typename = void>
constexpr const bool has_nested_type_v = false;
template<typename T>
constexpr const bool has_nested_type_v
<T, std::void_t<typename T::type>> = true;
Run Code Online (Sandbox Code Playgroud)
给定具有受保护或私有嵌套type类和简单程序的示例类型
#include "has_nested_type.hpp"
#include <iostream>
struct Protected {
protected:
struct type{};
};
struct Private {
private:
struct type{};
};
int main() {
std::cout << "Protected: "
<< (has_nested_type_v<Protected> ? "detected" : "not detected")
<< std::endl;
std::cout << "Private: "
<< (has_nested_type_v<Private> ? "detected" : "not detected")
<< std::endl;
}
Run Code Online (Sandbox Code Playgroud)
clang成功编译失败的检测(如预期的那样).该程序,编译和输出上再现wandbox 这里 …
我正在用 Rust 重写 C++ 解析器以获取旧版 ASCII 数据格式。这种格式的实数值允许以任何 Fortran 识别的格式存储。不幸的是,Fortran 可以识别 Rust(或大多数其他语言)无法识别的一些格式。例如,值 101.01 可能表示为
前三个都是 Rust 原生识别的。剩下的四个构成了挑战。在 C++ 中,我们使用以下例程来解析这些值:
double parse(const std::string& s){
char* p;
const double significand = strtod(&s[0], &p);
const long exponent = (*p == '\0') ?
0 : isalpha(*p) ?
strtol(p+1, nullptr) :
strtol(p, nullptr);
return significand * pow(10, exponent);
}
Run Code Online (Sandbox Code Playgroud)
在查看 Rust 文档后,标准库似乎没有以strtod和的方式提供部分字符串解析strtol。出于性能原因,我想避免多次传递字符串或使用正则表达式。
我在使用C++ 11 std::uniform_real_distribution编译Apple LLVM版本7.0.2(clang-700.1.81)时看到了一些奇怪的行为.调用operator()会在分布范围之外呈现结果.下面的最小样本程序再现了麻烦
// Example program
#include <random>
#include <iostream>
#include <string>
template< int power >
constexpr uint64_t power_of_two(){
return 2 * power_of_two< power - 1 >();
}
template< >
constexpr uint64_t power_of_two< 0 >(){
return 1;
}
std::linear_congruential_engine
< uint64_t, 273673163155, 13, power_of_two< 48 >() >
rng;
std::uniform_real_distribution< double > angle_cosine(-1, 1);
int main()
{
std::cout << angle_cosine.a() << " " << angle_cosine.b() << '\n' << std::endl;
for (int i = 0; i < 4; ++i){
std::cout …Run Code Online (Sandbox Code Playgroud) 我一直在和一对班级一起工作.前者存储元数据,后者作为容器,支持基于元数据的各种索引.剥离版本发布在下面.
后者类使用的std ::设置来管理其前级的对象的集合,以指涉稳定性方面的原因(如元素被添加和删除指针组成的元数据对象必须保持有效).
由于我不理解的原因,即使应该调用移动语义,索引类的set成员也会调用其(已删除的)复制构造函数.我已经在Apple LLVM 7.0.0(使用libc ++)和GCC 4.9(libstdc ++)上编译并收到了类似的错误.
是否有某些原因在这种情况下无法调用移动构造函数?
#include <ctime>
#include <functional>
#include <set>
#include <string>
#include <memory>
#include <vector>
// used to store meta data about some class T
template<typename T>
struct Foo {
std::unique_ptr<T> data; // T being some abstract class
std::string label;
std::time_t stamp;
const Foo* parentPtr;
Foo( std::unique_ptr<T>&& data,
const std::string& label,
const std::time_t stamp,
const Foo* parent ) : data( std::move(data) ),
label( label ),
stamp( stamp ),
parentPtr( parent ){}
};
// …Run Code Online (Sandbox Code Playgroud)