是否有任何理由再使用以下语法:
template<typename T>
auto access(T& t, int i)
-> decltype(t[i])
{
return t[i];
}
Run Code Online (Sandbox Code Playgroud)
现在我们可以使用:
template<typename T>
decltype(auto) access(T& t, int i)
{
return t[i];
}
Run Code Online (Sandbox Code Playgroud)
现在,尾随返回类型语法似乎有点多余?
我有一个过滤器功能列表。如果这些函数中的任何一个返回“ true”,则不应进一步处理事件。
std :: any_of似乎适合此用例,但我想保证按其添加到我的列表的顺序调用过滤器函数(因为它们可能会产生副作用)。因此,如果我使用std :: any_of,我需要知道它调用过滤器函数的顺序是确定性的,从列表的begin()到end()。
我已经检查了std :: any_of上的C ++标准和顺序执行策略,但是没有提到顺序保证。我没有找到关于cppreference的顺序保证的提法,也没有在stackoverflow上找到足够类似的答案。
我的结论是,没有订单保证。尽管大多数编译器可能会按顺序处理这些函数,但我不应该依赖它。
bool MyClass::event(QEvent* event)
{
std::vector<std::function<bool(QEvent*)> > functionList = getCurrentEventFilters();
const auto bFilter = std::any_of(functionList .begin(),
functionList .end(),
[&event](std::function<bool(QEvent*)> function) {return function(event);});
if (bFilter)
{
return true;
}
return Base::event(event);
}
Run Code Online (Sandbox Code Playgroud) 我的任务是创建一个函数来计算我的输入的反正弦。
我尝试使用 xcode 来调试它。一切正常,直到arcsin(new);调用返回为止。然后是一个segmentation fault: 11. 我不知道为什么,但float arcsin(floatvalue){ ... }运行第二个周期时的断点告诉我 float old 和 float value 是NAN。
float arcsin(float value){
float old = value;
float new = value + (0.5 * ((value * value * value)/3));
float accurate = 0.00001;
if ((new - old) < accurate){
return new;
}
else{
return arcsin(new);
}
}
int function_arcsin(int sigdig, float value){
value = arcsin(value);
printf("%.10e\n",value);
return 0;
}
Run Code Online (Sandbox Code Playgroud)