参考以下代码
test_linker.cpp
int main() {
srand(time(0));
for (int i = 0; i < 10; ++i) {
cout << rand() % 10 << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
urandom.cpp
#include <iostream>
using std::cout;
using std::endl;
#include <dlfcn.h>
int rand() throw() {
// get the original rand() function
static auto original_rand = (decltype(&rand)) dlsym(RTLD_NEXT,"rand");
cout << "Call made to rand()" << endl;
return original_rand();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下命令编译代码时
g++ -std=c++11 -Wall -Werror -Wextra -Wvla -pedantic -O3 urandom.cpp -c
g++ -std=c++11 -Wall -O3 test_linker.cpp urandom.o …Run Code Online (Sandbox Code Playgroud) 我已经阅读了很多关于可变参数模板和std :: bind的帖子,但我想我仍然不理解它们如何协同工作.我认为我的概念在使用可变参数模板时会有点模糊,std :: bind用于什么以及它们如何结合在一起.
在下面的代码中,我的lambda使用点运算符和TestClass类型的对象,但即使我传入std :: ref类型的对象,它们仍然可以工作.这到底是怎么回事?隐式转换是如何发生的?
#include <iostream>
using std::cout;
using std::endl;
#include <functional>
#include <utility>
using std::forward;
class TestClass {
public:
TestClass(const TestClass& other) {
this->integer = other.integer;
cout << "Copy constructed" << endl;
}
TestClass() : integer(0) {
cout << "Default constructed" << endl;
}
TestClass(TestClass&& other) {
cout << "Move constructed" << endl;
this->integer = other.integer;
}
int integer;
};
template <typename FunctionType, typename ...Args>
void my_function(FunctionType function, Args&&... args) {
cout << "in function" …Run Code Online (Sandbox Code Playgroud) 参考以下代码
#include <utility>
#include <cassert>
template <typename T>
struct Wot;
template <int... ints>
struct Wot<std::index_sequence<ints...>> {};
int main() {
assert(sizeof(Wot<std::index_sequence<1, 2, 3>>) == 1);
}
Run Code Online (Sandbox Code Playgroud)
这适用于clang但不适用于gcc,当我std::size_t在索引序列中更改要接受的部分特化的类型时,它可以工作.
谁是对的?Clang还是gcc?
条件变量应该具有相对于的单个顺序notify()和unlock_sleep()(wait()在互斥锁被解锁并且线程作为一个原子操作序列休眠的操作中使用的虚函数调用)操作.要使用任意可锁定实现此功能,std::condition_variable_any通常在内部使用另一个互斥锁(以确保原子性和睡眠状态)
如果内部unlock_sleep()和notify()(notify_one()或notify_all())操作相对于彼此不是原子操作,则冒着线程解锁互斥锁的风险,另一个线程发出信号,然后原始线程将进入休眠状态并且从不唤醒.
我正在阅读std :: condition_variable_any的libstdc ++和libc ++实现,并注意到libc ++实现中的这段代码
{lock_guard<mutex> __lx(*__mut_);}
__cv_.notify_one();
Run Code Online (Sandbox Code Playgroud)
内部互斥锁被锁定,然后在信号操作之前立即解锁.这不是我上面描述的问题吗?
为什么以下代码无法编译?即使这样做也是合法的void* ptr = 0;
template <void* ptr = 0>
void func();
int main() {
func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我问,因为我发现一个非常值得信赖的来源做了类似的事情,但无法在我的机器上编译
注意应该已经发布了编译器错误以及我的问题,所以在这里
so_test.cpp:1:23: error: null non-type template argument must be cast to template parameter type 'void *'
template <void* ptr = 0>
^
static_cast<void *>( )
so_test.cpp:1:17: note: template parameter is declared here
template <void* ptr = 0>
^
so_test.cpp:5:5: error: no matching function for call to 'func'
func();
^~~~
so_test.cpp:2:6: note: candidate template ignored: substitution failure [with ptr = …Run Code Online (Sandbox Code Playgroud) 我浏览了结构化绑定的论文http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0144r0.pdf,但我无法很好地了解哪种类型的结构语法允许绑定到.我最好的猜测是结构必须是聚合类型.或者只有公共数据成员的东西.
我有什么警告吗?
我正在阅读std::for_each这里的文档http://en.cppreference.com/w/cpp/algorithm/for_each并看到返回值是std::move(f)
为什么标准强制在返回值中移动输入参数?默认情况下不会移动它,因为输入参数是按值传递的吗?
当您编译以下代码时,这会引导我进行几次跟进
Something function(Something something) {
return something;
}
Run Code Online (Sandbox Code Playgroud)
return语句是我的系统上具有最高优化级别(-O3)的移动,为什么大多数编译器都不会忽略此返回值?省略了本地值,但函数参数不是..
在这种情况下C++ 17是否强制执行?我阅读了该提案(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0135r0.html)但我并不完全了解哪些案例符合强制性省略的条件.
我Apple LLVM version 8.0.0 (clang-800.0.42.1)在我的Mac和g++ 5.4Ubuntu 16.04 上试过这个.
分段堆栈如何工作?这个问题也适用于Boost.Coroutine我所以我也在使用C++标签.主要的疑问来自这篇文章看起来他们所做的是在堆栈的底部保留一些空间并通过在那里分配的内存(可能通过mmap和mprotect?)注册某种信号处理程序来检查它是否已经损坏了当他们发现他们已经耗尽了空间时,他们继续分配更多的内存,然后从那里继续.3个问题
这不是构建用户空间的东西吗?它们如何控制新堆栈的分配位置以及如何编译程序的指令以了解它?
push指令基本上只是向堆栈指针添加一个值,然后将值存储在堆栈中的寄存器中,那么push指令如何知道新堆栈的启动位置以及相应的pop如何知道它何时必须将堆栈指针移回旧堆栈?
他们也说
在我们得到一个新的堆栈段之后,我们
goroutine通过重试导致我们用完堆栈的函数来重新启动它
这是什么意思?他们重启整个goroutine吗?这不可能导致非确定性行为吗?
他们如何检测到程序已超出堆栈?如果他们在底部保留一个canary-ish内存区域,那么当用户程序创建一个足够大的数组溢出时会发生什么?这不会导致堆栈溢出并且是一个潜在的安全漏洞吗?
如果Go和Boost的实现不同,我很高兴知道它们中的任何一个如何处理这种情况
在C ++中是否存在预处理器宏,常量或类似的东西可用于判断翻译单元是否正在使用分段堆栈(例如-fsplit-stack)进行编译?
任何编译器的答案都很好,但是我特别在寻找clang和gcc
以下代码应该只创建thread_local一次in-class ,但最终会在每次访问时初始化它
#include <iostream>
#include <thread>
using std::cout;
using std::endl;
template <typename T>
class Something {
public:
struct TLBookkeeping {
TLBookkeeping() {
std::cout << "TLBookkeeping() " << std::this_thread::get_id() << std::endl;
}
};
static void foo();
static thread_local TLBookkeeping bookkeeping_;
};
template <typename T>
thread_local typename Something<T>::TLBookkeeping Something<T>::bookkeeping_;
template <typename T>
void Something<T>::foo() {
std::cout << &bookkeeping_ << std::endl;
std::cout << &bookkeeping_ << std::endl;
}
namespace {
struct Struct {};
}
int main() {
Something<Struct>::foo();
}
Run Code Online (Sandbox Code Playgroud)
c++ ×10
c++11 ×5
c++14 ×4
c++17 ×3
templates ×3
clang ×1
copy-elision ×1
fiber ×1
go ×1
goroutine ×1
linux ×1
move ×1
portability ×1
thread-local ×1
void ×1