我在这里偶然发现了这段代码.
Generators doubleSquares(int value)
{
Generators result;
for (int i = 0; i <= std::sqrt(value / 2); i++) // 1
{
double j = std::sqrt(value - std::pow(i, 2)); // 2
if (std::floor(j) == j) // 3
result.push_back( { i, static_cast<int>(j) } ); // 4
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
我错误地认为// 3是危险的吗?
让我们说要求是这样的:作为一个类用户,我想收集关于一个主题的信息,当类有足够的信息时,我希望一个类能够将收集的数据列表返回给我.足够的信息被定义为 - 当收集来自所有可能信息的子集的所有信息时.该子集不是固定的,而是提供给该类.
例如,这是所有可能信息的列表:
{
string name;
int age;
char sex;
string location;
}
Run Code Online (Sandbox Code Playgroud)
我想让我的用户有机会告诉我从一些数据源(我的班级解析数据)收听,直到我得到年龄和性别.
问题是我不知道如何在没有枚举的情况下传达它.基本上我的枚举解决方案是监听数据源,直到我确定在我收集了所有数据的2组枚举(收集,必需)上使用std :: includes.
没有枚举可以做到吗?
Boost.Asio udp::endpoint有一个远程地址的成员.因为我正在监听多个接口(如下所示):
udp_socket(io_service, udp::endpoint(udp::v4(), port))
Run Code Online (Sandbox Code Playgroud)
在我的处理程序中,我不知道哪个网络接口收到了数据包.
如果没有遍历网络接口并在每个接口上寻找端点地址和我的IP之间的相似性,我可以从我收到消息的接口获取IP吗?
最近发布在Stack Overflow上的答案显示了代码,它为标准算法提供了一个采用不同类型操作数的比较器:
2.使用带模板的比较器
operator().而不是使用lambda,定义一个带模板的仿函数
operator().Run Code Online (Sandbox Code Playgroud)struct comparator { template<typename T, typename U> bool operator()(T const& lhs, U const& rhs) const { return lhs.mCommonField < rhs.mCommonField; } };然后,它就像:
Run Code Online (Sandbox Code Playgroud)std::sort(aStructs.begin(), aStructs.end(), comparator{}); std::sort(bStructs.begin(), bStructs.end(), comparator{}); // ... std::set_intersection(aStructs.begin(), aStructs.end(), bStructs.begin(), bStructs.end(), std::back_inserter(intersection), comparator{} );请注意,由于比较器中有模板,因此必须在函数范围之外声明.Coliru Viewer上的实例.
显然,这至少在实践中有效,正如工作现场演示所证明的那样.
但标准是否严格允许?
首先,这是一个好奇的问题,我永远不会在现实生活中编写这样的代码。
以下代码与 -O3 -std=c++14 和 -O3 -std=c++17 标志的行为不同,在 C++14 中,我得到了错误的分配,我推测是从垃圾 std::string 的复制构造:
#include<algorithm>
#include<numeric>
#include<vector>
#include<string>
#include<iostream>
using namespace std;
static auto results = std::initializer_list<string>{"1 ",
"2"};
string f() {
auto result = std::accumulate(results.begin(), results.end(), string(""));
return result;
}
int main()
{
return f().size();
}
Run Code Online (Sandbox Code Playgroud)
我的猜测是 C++17 版本比 C++14 版本保持底层数组的存活时间更长,但我发现 cppreference 上的初始化列表从 C++14 到 C++17 没有相关变化,所以我很困惑。这只是 UB 是 UB,还是语言发生了变化?
PS我知道如何解决这个问题,使用static const auto& results作品,就像之前提到的,这只是一个关于语言极端情况的问题。
注意:我认为这在技术上与这个问题重复,但是:
==相当激进,我不确定恢复 9 年的问题是否是正确的做法。==符。<=><ps 我目前有自己的看法(基于 foonathan 的一些谈话),但这只是当前的偏好,我不希望以此来偏见潜在的答案。
我知道这std::vector<bool>很糟糕,但我有一个非常好的用例。
我想做这样的事情:
uint8_t data [] = {7,32};
std::vector<bool> vb/*...*/ ;
// now vb is size of 8 vector with this values:
// false, false, false, false, false, true, true, true, (7)
// false, false, true, false, false, false, false, false (32)
Run Code Online (Sandbox Code Playgroud)
注意:我知道我可以手动解析输入的每个字节并进行 8 次插入,但我正在寻找标准中预先构建的内容
考虑以下代码:
struct A{
int x;
int y;
};
struct B{
int y;
int x;
};
void func (A){
}
void func (B){
}
int main()
{
func({.y=1,.x=1});
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,clang 和 gcc 都认为这段代码是不明确的,尽管已知指定初始值设定项中的顺序必须与 struct 中的顺序相匹配,尽管出于某种原因,clang 允许它并且只是发出警告:
ISO C++ 要求在声明顺序中指定字段指示符;字段 'y' 将在字段 'x' [-Wreorder-init-list] 之后初始化
现在有趣开始了:如果我注释掉func(B)代码,代码就会从模棱两可变成无法编译。
这就是我认为超级奇怪的地方。这背后有什么逻辑吗?
如果我的困惑的原因不清楚:
如果我们有两个func(A)和func(B)代码GCC和铛考虑func({.y=1,.x=1})暧昧,但如果我删除func(B)从源则给出了一个错误(或警告铛的情况下)。换句话说,我们通过删除 1 个选项从 2 个选项变为 0 个选项。
这段代码来自另一个问题的回答:
template <typename F, std::size_t ... Is>
constexpr auto apply(F f, std::index_sequence<Is...>)
-> std::index_sequence<f(Is)...>
{
return {};
}
Run Code Online (Sandbox Code Playgroud)
gcc 失败
Run Code Online (Sandbox Code Playgroud)<source>:5:29: error: expected parameter pack before '...'
msvc 和 clang 编译它。
现在将其更改为此会导致 msvc 失败:
template <typename F, std::size_t ... Is>
constexpr auto apply(F , std::index_sequence<Is...>)
-> std::index_sequence<F{}(Is)...>
{
return {};
}
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)<source>(5): error C2187: syntax error: '<end Parse>' was unexpected here <source>(5): error C2059: syntax error: '(' <source>(6): error C2988: unrecognizable template declaration/definition <source>(6): error C2059: syntax error: '{' …
我有一个以下有缺陷的程序。逻辑是无稽之谈,这只是一个玩具例子。
#include <ranges>
#include <iostream>
#include <fmt/format.h>
#include <fmt/ranges.h>
template<typename T>
constexpr bool size_is_4(){
return sizeof(T)==4;
}
int main() {
std::cout << fmt::format("float size is 4 bytes : {}\n", size_is_4<float>);
std::cout << fmt::format("double size is 4 bytes : {}\n", size_is_4<double>);
}
Run Code Online (Sandbox Code Playgroud)
输出是
float 大小为 4 个字节:true
double 大小为 4 个字节:true
问题是我传递了一个函数指针fmt::format并将其作为布尔值打印出来。修复很简单,只需调用该函数即可,但我想知道是否有一种方法可以捕获这样的错误。
由于函数返回,bool它作为输出实际上看起来很合理。