#include <boost/type_index.hpp>
#include <iostream>
#include <vector>
int main()
{
std::vector v{2, 3.14};
std::cout << boost::typeindex::type_id_with_cvr<decltype(v)>().pretty_name() << '\n';
std::cout << "size: " << v.size() << '\n';
for (auto x : v)
std::cout << "- " << x << '\n';
}
Run Code Online (Sandbox Code Playgroud)
输出是:
#include <boost/type_index.hpp>
#include <iostream>
#include <vector>
int main()
{
std::vector v{2, 3.14};
std::cout << boost::typeindex::type_id_with_cvr<decltype(v)>().pretty_name() << '\n';
std::cout << "size: " << v.size() << '\n';
for (auto x : v)
std::cout << "- " << x << '\n';
}
Run Code Online (Sandbox Code Playgroud)
std::vector …
在以下代码(gcc 10.2.1)中,调用regex_match返回“不匹配”,我认为这是正确的。
sm.size()返回0,但是当从sm.begin()to迭代时end(),它找到了 3 次出现(都是空字符串)。
如果这是正确的,这三个发现是什么意思?
但既然size()==0,不应该begin() == end()吗?
编辑:根据评论,我ready在输出中添加了标志
#include <iostream>
#include <string>
#include <regex>
#include <assert.h>
int main()
{
std::string input("4321");
std::regex rg("^([0-9])");
std::smatch sm;
bool found = std::regex_match(input, sm, rg);
assert(!sm.size() == sm.empty());
std::cout << "ready: " << sm.ready() << ", found: " <<
found << ", size: " << sm.size() << std::endl;
for (auto it = sm.begin(); it != sm.end(); ++it) …Run Code Online (Sandbox Code Playgroud) C++标准对"xvalues"(N4762§7.2.1.4)进行了以下说明:
表达式是xvalue,如果它是:
- ...
- 类成员访问表达式,指定非引用类型的非静态数据成员,其中对象表达式是xvalue
考虑以下代码片段(使用Boost打印表达式的类型):
#include <iostream>
#include <boost/type_index.hpp>
using boost::typeindex::type_id_with_cvr;
struct X {
int var;
} x;
int main()
{
auto extended_type = type_id_with_cvr<decltype( std::move(x).var )>();
std::cout << extended_type.pretty_name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是关于表达式std::move(x).var:
基于标准中的文本,我希望表达式是xvalue,但输出int不是int &&.
我在这里错过了什么?
在我的平台(X86、Fedora、gcc 9.1.1)sig_atomic_t上,类型定义为普通的int.
在 C++ 标准中,sig_atomic_t始终与volatile限定符一起使用。
我明白为什么volatile需要,但为什么它不是类型的一部分呢?
就像是:
using sig_atomic_t = volatile int;
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
#include <thread>
#include <iostream>
#include <future>
std::promise<int> prom;
void thr_func(int n)
{
prom.set_value(n + 10);
}
int main()
{
std::thread t{thr_func, 5};
auto fut = prom.get_future();
int result = fut.get();
std::cout << result << std::endl;
t.join();
}
Run Code Online (Sandbox Code Playgroud)
prom同时访问该对象,即使标准说它set_value是原子的,我也找不到任何关于get_future原子(或const)的东西.
因此我想知道这样称呼是否正确get_future.
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
class test : public enable_shared_from_this<test> {
vector<shared_ptr<test>> vec;
public:
void addvec()
{
vec.emplace_back(shared_from_this());
}
~test() { cout << "destructing\n"; }
};
int main()
{
auto sp = make_shared<test>();
sp->addvec(); // when called, destructor is not invoked and
// LeakSanitizer reports a memory leak
}
Run Code Online (Sandbox Code Playgroud)
对象test由 管理std::shared_ptr,并且在调用 后addvec,第二个shared_ptr实例将存储在内部向量中 (ref_count == 2)。
但析构函数从未被调用,并且报告了内存泄漏。为什么是这样?
使用g++ -g -fsanitize=leak c.cpp
GCC 版本 12.2.1编译
#include <boost/type_index.hpp>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> initv { 42, 31, 7 };
std::vector v1{initv.begin(), initv.end()}; // CTAD
std::vector<int> v2{initv.begin(), initv.end()};
std::cout << boost::typeindex::type_id_with_cvr<decltype(v1)>().pretty_name() << std::endl;
std::cout << boost::typeindex::type_id_with_cvr<decltype(v2)>().pretty_name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
#include <boost/type_index.hpp>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> initv { 42, 31, 7 };
std::vector v1{initv.begin(), initv.end()}; // CTAD
std::vector<int> v2{initv.begin(), initv.end()};
std::cout << boost::typeindex::type_id_with_cvr<decltype(v1)>().pretty_name() << std::endl;
std::cout << boost::typeindex::type_id_with_cvr<decltype(v2)>().pretty_name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
CTAD 生成带有迭代器的向量,而不是整数。
鉴于std::vector有一个采用迭代器的构造函数,这是正确的吗?