我需要将std :: function传递给某些算法.功能的类型是
typedef std::function<bool(const double&)> Condition;
Run Code Online (Sandbox Code Playgroud)
在最简单的情况下,此函数将如下所示
bool simpleCondition(const double& d){return d<0.001;}
Run Code Online (Sandbox Code Playgroud)
现在我想传递相同的条件,但只有当条件连续多次填满时,该函数才会返回true.我尝试了以下内容
class RepeatingCondition{
public:
static Condition getRepeatingCondition(Condition c,int reps){
return std::bind(&RepeatingCondition::evalCondition,
RepeatingCondition(c,reps),_1);
}
private:
RepeatingCondition(Condition cc,int reps) : counter(0),
reps(reps),cond(cc){}
bool evalCondition(const double& d){
if (cond(d)){counter += 1;}
else {counter = 0;}
return (counter >= reps);
}
Condition cond;
int counter,reps;
};
Run Code Online (Sandbox Code Playgroud)
我的编译器没有抱怨,它似乎按预期工作.但是,我真的不明白为什么(使用简单的函数指针它不起作用,对吧?).另外,我想知道是否有更简单的方法来实现同样的目标.
当我运行这个时:
int main() {
unsigned a = 5;
std::cout << -a << std::endl;
int b = -a;
std::cout << b << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我明白了:
4294967291
-5
Run Code Online (Sandbox Code Playgroud)
看起来它有效,我可以取 an 的负数unsigned
并将其分配给 an int
,但这真的总是可以吗?为什么?
当我尝试一些对我来说看起来类似的情况时:
int c = 1;
int d = 3;
double x = c/d;
std::cout << x << std::endl;
Run Code Online (Sandbox Code Playgroud)
我明白了0
(正如预期的那样)。
PS:也许有一个骗局,但我没有找到它,我能找到的最接近的是这个
考虑这段代码
#include <iterator>
#include <vector>
const int& foo(const std::vector<int>& x,unsigned i) {
auto it = x.begin();
std::advance(it,i);
return *it;
}
Run Code Online (Sandbox Code Playgroud)
clang和gcc都没有发出错误/警告,但是:
#include <iterator>
#include <map>
const std::pair<int,int>& bar(const std::map<int,int>& x,unsigned i){
auto it = x.begin();
std::advance(it,i);
return *it;
}
Run Code Online (Sandbox Code Playgroud)
用clang编译并使用-Werror
结果:
<source>:14:12: error: returning reference to local temporary object [-Werror,-Wreturn-stack-address]
return *it;
^~~
Run Code Online (Sandbox Code Playgroud)
和gcc:
<source>: In function 'const std::pair<int, int>& bar(const std::map<int, int>&, unsigned int)':
<source>:14:13: error: returning reference to temporary [-Werror=return-local-addr]
return *it;
^~ …
Run Code Online (Sandbox Code Playgroud) 我如何编写一个像这样使用的宏(用于 gcc):
CREATE_STRUCT(my_struct1,foo);
CREATE_STRUCT(my_struct2,foo,bar);
Run Code Online (Sandbox Code Playgroud)
并扩展到
struct my_struct1 {
std::string foo;
};
struct my_struct2 {
std::string foo;
std::string bar;
};
Run Code Online (Sandbox Code Playgroud)
?
我当然需要不同数量的成员的灵活性,但对我来说已经很小的数量就可以了(比如 4 或 5)。
我发现了几个相关的问题,例如this和this,但是在尝试将这种神秘的宏魔法应用于这个问题时,我完全迷失了。
PS:我知道如何编写 5 个宏(每个参数一个)来完成这项工作,所以实际上问题是:是否有一种“简单”的方法来编写一个可以完成这项工作的可变参数宏?另一方面,我将向结构中添加更多内容,因此将所有内容放在一个地方会节省大量样板。
我想在整个双范围内生成随机数.这样做
std::uniform_real_distribution<double> doubleDist(std::numeric_limits<double>::lowest(),
std::numeric_limits<double>::max());
Run Code Online (Sandbox Code Playgroud)
然而,产生IND/NaN.我花了一段时间才明白这是根据文档:
需要
a ? b
和b-a ? std::numeric_limits<RealType>::max()
显然std::numeric_limits<double>::max() - std::numeric_limits<double>::lowest()
大于::max
,因为:lowest
是负的(不像::min
).
为什么我不能以这种方式在最低和最高之间生成数字,背后的理由是什么?
我刚刚发现在库基础知识 TS v2 中有一个make_array
( std::experimental::make_array
) 模板,可以从其参数中推断出数组类型。我想主要目的是启用auto
类似于 cppreference 示例的用法:
auto x = std::experimental::make_array(1,2,3,4,5);
Run Code Online (Sandbox Code Playgroud)
有没有我没有看到的不同动机?
有了 C++20 中新的类型推导工具,人们还能期待make_array
有一天它会成为标准,还是已经过时了?
我很难理解这段代码。我完全理解为什么 foo() 打印这些值,但我无法理解为什么 bar() 反向打印这些值。任何人都可以请无论如何解释这一点,以便我可以直观地感受到它,或者至少给我一个方向以达到赦免。
#include<iostream>
using namespace std;
void bar(int a){
cout<<"bar: "<<a<<endl;
}
void foo(int a){
if(a>0){
a -=1;
cout<<"foo: "<<a<<endl;
foo(a);
bar(a);
}else{
cout<<"Foo exited"<<endl;
}
}
int main(){
foo(10);
}
[Output]:
foo: 9
foo: 8
foo: 7
foo: 6
foo: 5
foo: 4
foo: 3
foo: 2
foo: 1
foo: 0
Foo exited
bar: 0
bar: 1
bar: 2
bar: 3
bar: 4
bar: 5
bar: 6
bar: 7
bar: 8
bar: 9
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
#include <iostream>
template <template<class...> class C>
struct foo {
foo() { std::cout << "base case\n";}
};
template <template<class> class C>
struct foo< C > {
foo() { std::cout << "single param case\n";}
};
template <template<class,class> class C>
struct foo< C > {
foo() { std::cout << "two param case\n";}
};
template <typename T> struct bar1 {};
template <typename T,typename U> struct bar2 {};
template <typename T,typename U,typename V> struct bar3 {};
template <typename...T> struct barN {};
int main() { …
Run Code Online (Sandbox Code Playgroud) 以下程序编译为 C 程序:
\n#include <stdlib.h>\n#include <stdio.h>\n\nvoid f(int n, int m, int x[n][m]) {\n printf("x[0][2] = %i\\n",x[0][2]);\n}\n\nint main() {\n int v[][3] = { {0,1,2}, {3,4,5} };\n\n f(2,3,v);\n}\n
Run Code Online (Sandbox Code Playgroud)\n然而,当用 g++ 编译为 C++ 时,我有:
\nmain.c:4:29: error: use of parameter outside function body before \xe2\x80\x98]\xe2\x80\x99 token\n void f(int n, int m, int x[n][m]) {\n ^\n
Run Code Online (Sandbox Code Playgroud)\n看来C的这个特性在C++中并不存在。是否可以向 g++ 提供任何标志以使其接受代码?
\n我正在尝试使用概念来重载其参数取决于模板参数的模板化函数。不幸的是,这在 gcc 上失败了,报告了一个不明确的过载。
在我的项目中,我的代码在 gcc 9.3 中编译,但在 gcc 12.2 中失败。但在尝试最小化代码时,我最终得到了这个,这在两个 gcc 版本上都失败了,但在 clang 15.0.0 中有效:
#include <type_traits>
#include <iostream>
struct A
{
using value_type = int;
};
struct B
{
using value_type = int;
};
template<typename Candidate>
concept something = requires {
typename Candidate::value_type;
};
template<typename Candidate>
concept something_specific = something<Candidate> && std::is_same_v<Candidate, A>;
template<something T>
void foo()
{
std::cout << "Something" << std::endl;
}
template<something_specific T>
void foo()
{
std::cout << "Something specific" << std::endl;
} …
Run Code Online (Sandbox Code Playgroud)