根据某种条件在几个已知范围之一上进行迭代的最有效方法是什么?
二进制条件的伪代码:
for element in (condition ? range_a : range_b)
// do work
Run Code Online (Sandbox Code Playgroud)
这个“示例”显示了我使用基于范围的for循环的意图,但由于std::initializer_list具有参考语义,因此无法使用。
for element in (condition ? range_a : range_b)
// do work
Run Code Online (Sandbox Code Playgroud)
产量: warning: returning address of local temporary object [-Wreturn-stack-address]
在运行时,我可以返回a,std::vector但这将涉及在每次调用时构造一个新的向量:
constexpr auto some_range(bool c) -> std::initializer_list<int> {
if (c) {
return {1,2};
} else {
return {3, 4, 5};
}
}
bool cond = true; // false
for(auto x : some_range(cond)) {
// things
}
Run Code Online (Sandbox Code Playgroud)
我可以使用的固定大小std::array …
我的std::variant包含可流式类型:
std::variant<int, std::string> a, b;
a = 1;
b = "hi";
std::cout << a << b << std::endl;
Run Code Online (Sandbox Code Playgroud)
使用带有-std = c ++ 1z的g ++ 7进行编译会返回编译时错误.
摘录:
test.cpp: In function 'int main(int, char**)':
test.cpp:10:13: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::variant<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >')
std::cout << a << b << std::endl;
~~~~~~~~~~^~~~
Run Code Online (Sandbox Code Playgroud)
貌似std::variant<int, std::string>是不能流.我怎样才能实现我可以直接将变量流式传输到输出流?
预期产量:
1hi
Run Code Online (Sandbox Code Playgroud) GCC 支持-fconcepts切换,提供实验性核心语言概念功能。
还有一种方法可以使用标准库实现(的实验版本)吗?
在设计概念时如果有std::derived_from、std::convertible_to、std::destructible 和朋友们在场那就太好了。
#include <concepts>
template<typename T>
concept Fooable = requires(T f) {
{ bar(f) } -> std::convertible_to<float>;
};
Run Code Online (Sandbox Code Playgroud) 我有一个取决于模板template参数的类型:
template<typename X_, template<typename, typename> class Y_>
struct A { /*...*/ };
Run Code Online (Sandbox Code Playgroud)
我用工厂函数构造的:
template<typename X_, template<typename, typename> class Y_>
A<X_, Y_> make() {
return A<X_, Y_> { /*...*/ };
};
Run Code Online (Sandbox Code Playgroud)
现在,我想添加一个工厂函数,该函数接受一个Aby引用并分配给它,而无需重新声明其模板参数:
template<typename A_>
void make(A_& a) {
a = make<typename A_::X, typename A_::Y>();
}
Run Code Online (Sandbox Code Playgroud)
我添加了两个模板别名A来实现此目的:
template<typename X_, template<typename, typename> class Y_>
struct A {
using X = X_;
template <typename... T>
using Y = Y_<T...>;
/* ... */
};
Run Code Online (Sandbox Code Playgroud)
然后我尝试编译:
A<int, std::vector> v;
make(v); …Run Code Online (Sandbox Code Playgroud) std::ratio 为度量前缀(centi、deci、deca、hecto)提供方便的 typedef。
yocto std::ratio<1, 1000000000000000000000000>, if std::intmax_t can represent the denominator
zepto std::ratio<1, 1000000000000000000000>, if std::intmax_t can represent the denominator
atto std::ratio<1, 1000000000000000000>
femto std::ratio<1, 1000000000000000>
pico std::ratio<1, 1000000000000>
nano std::ratio<1, 1000000000>
micro std::ratio<1, 1000000>
milli std::ratio<1, 1000>
centi std::ratio<1, 100>
deci std::ratio<1, 10>
deca std::ratio<10, 1>
hecto std::ratio<100, 1>
kilo std::ratio<1000, 1>
mega std::ratio<1000000, 1>
giga std::ratio<1000000000, 1>
tera std::ratio<1000000000000, 1>
peta std::ratio<1000000000000000, 1>
exa std::ratio<1000000000000000000, 1>
zetta std::ratio<1000000000000000000000, 1>, if std::intmax_t can represent the numerator
yotta std::ratio<1000000000000000000000000, …Run Code Online (Sandbox Code Playgroud)