考虑以下两个命令来构建一个简单的可执行文件
$ gcc -g -Wall -Wl,--enable-new-dtags -Wl,-rpath,'$ORIGIN'/sharedLibDir -o prog main.c ./sharedLibDir/libdemo.so
$ gcc -g -Wall -Wl,--enable-new-dtags -Wl,-rpath,./sharedLibDir -o prog main.c ./sharedLibDir/libdemo.so
Run Code Online (Sandbox Code Playgroud)
显然,一个使用本地目录作为RPATH,另一个使用$ ORIGIN.我看不出这两者之间有什么区别(除了二进制文件中RPATH和RUNPATH的值); 两者都允许移动可执行文件,只要它有一个名为sharedLibDir的并行目录,它就会运行.
$ ORIGIN有什么意义?它是否有一些我错过的附加功能?提前致谢.
请考虑以下代码:
class CMyClass {};
template<class T>
void func(T&& param) {
if (std::is_same<CMyClass, std::decay<T>::type>::value)
std::cout << "param is a CMyClass\n";
if (std::is_same<T, CMyClass&>::value)
std::cout << "param is a CMyClass reference\n";
else if (std::is_same<T, CMyClass&&>::value)
std::cout << "param is a CMyClass r-value reference\n";
else if (std::is_same<T, const CMyClass&>::value)
std::cout << "param is a const CMyClass reference\n";
else if (std::is_same<T, const CMyClass&&>::value)
std::cout << "param is a const CMyClass r-value reference\n";
else if (std::is_same<T, const CMyClass>::value)
std::cout << "param is a constant CMyClass\n"; …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
template<int nIndex>
int Fibonacci()
{
if constexpr (nIndex == 0) return 0;
if constexpr (nIndex == 1) return 1;
static_assert(nIndex >= 0, "Invalid index passed to Fibonacci()");
return Fibonacci<nIndex - 1>() + Fibonacci<nIndex - 2>();
}
int main()
{
Fibonacci<3>(); // 2
//Fibonacci<-1>(); // Fires assertion
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我在VS2017中运行它时,编译器输出:
error C2338: Invalid index passed to Fibonacci()
note: see reference to function template instantiation 'int Fibonacci<-1>(void)' being compiled
note: see reference to function template instantiation 'int Fibonacci<1>(void)' being compiled …Run Code Online (Sandbox Code Playgroud) 考虑到我有一个std::atomic<long>变量和两个写入变量的线程。我有一台多核机器,所以这两个线程真正并发运行。
假设写入恰好同时发生(如果可能的话),有什么机制可以确保两次写入的结果不会以任何方式交错?
假设我已经打开dev/poll mDevPoll,我可以安全地调用这样的代码
struct pollfd tmp_pfd;
tmp_pfd.fd = fd;
tmp_pfd.events = POLLIN;
// Write pollfd to /dev/poll
write(mDevPoll, &tmp_pfd, sizeof(struct pollfd));
Run Code Online (Sandbox Code Playgroud)
...同时来自多个线程,或者我是否需要添加自己的同步原语mDevPoll?
c++ ×3
c++11 ×2
c ×1
c++17 ×1
concurrency ×1
constexpr ×1
gcc ×1
if-statement ×1
solaris ×1
solaris-10 ×1
templates ×1
unix ×1