小编Wad*_*Wad的帖子

相对路径之间的差异和使用$ ORIGIN作为RPATH

考虑以下两个命令来构建一个简单的可执行文件

$ 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有什么意义?它是否有一些我错过的附加功能?提前致谢.

gcc

2
推荐指数
1
解决办法
1673
查看次数

类型不推断为r值参考:为什么不呢?

请考虑以下代码:

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)

c++ c++11

2
推荐指数
1
解决办法
161
查看次数

斐波那契和'if constexpr'

请考虑以下代码:

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)

c++ templates if-statement constexpr c++17

2
推荐指数
1
解决办法
186
查看次数

std::atomic 和多核处理器

考虑到我有一个std::atomic<long>变量和两个写入变量的线程。我有一台多核机器,所以这两个线程真正并发运行。

假设写入恰好同时发生(如果可能的话),有什么机制可以确保两次写入的结果不会以任何方式交错?

c++ concurrency multithreading c++11

1
推荐指数
1
解决办法
686
查看次数

write()是否可以安全地同时从多个线程调用?

假设我已经打开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 unix multithreading solaris solaris-10

1
推荐指数
2
解决办法
1422
查看次数