小编Lor*_*one的帖子

Linux功能(setcap)似乎禁用了LD_LIBRARY_PATH

我用来LD_LIBRARY_PATH为应用程序设置某个用户库的路径.但是如果我在这个应用程序上设置功能

sudo setcap CAP_NET_BIND_SERVICE=eip myapplication
Run Code Online (Sandbox Code Playgroud)

然后LD_LIBRARY_PATH似乎被忽略了.当我启动程序时,Linux抱怨它无法找到某个共享库.

我猜这里有一些保护措施,以防止具有扩展权限的应用程序被劫持.有解决方法吗?

linux shared-libraries linux-capabilities

26
推荐指数
4
解决办法
1万
查看次数

按价值类成员捕获

在成员函数中编写lambda函数时,有没有办法按值捕获封闭类的字段?默认的catch-all =不起作用,因为当我在lambda中引用变量时,我得到的是从捕获的this指针中取消引用,以及在捕获列表中显式命名变量,因为我得到两个编译错误:capture of non-variable <name>,和‘this’ was not captured for this lambda function

c++ lambda closures c++11

26
推荐指数
2
解决办法
2万
查看次数

通用成员函数指针作为模板参数

考虑以下代码:

#include <iostream>
using namespace std;

class hello{
public:
    void f(){
        cout<<"f"<<endl;
    }
    virtual void ff(){
        cout<<"ff"<<endl;
    }
};

#define call_mem_fn(object, ptr)  ((object).*(ptr))

template<R (C::*ptr_to_mem)(Args...)> void proxycall(C& obj){
    cout<<"hello"<<endl;
    call_mem_fn(obj, ptr_to_mem)();
}

int main(){
    hello obj;
    proxycall<&hello::f>(obj);
}
Run Code Online (Sandbox Code Playgroud)

当然,这不会在第16行编译,因为编译器不知道是什么R,C并且Args,是.但还有另一个问题:如果有人试图在之前定义这些模板参数ptr_to_mem,他会遇到这种糟糕的情况:

template<typename R, typename C, typename... Args, R (C::*ptr_to_mem)(Args...)> 
                             //  ^variadic template, but not as last parameter!
void proxycall(C& obj){
    cout<<"hello"<<endl;
    call_mem_fn(obj, ptr_to_mem)();
}

int main(){
    hello obj;
    proxycall<void, hello, &hello::f>(obj);
}
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是,g ++没有抱怨 …

c++ function-pointers variadic-templates c++11

24
推荐指数
1
解决办法
3万
查看次数

模板重载和SFINAE只能处理函数而不能处理类

有人可以解释为什么编译器只接受这个代码

template<typename L, size_t offset, typename enable_if< (offset<sizeof(L)), int >::type =0>
void a_function(){}

template<typename L, size_t offset, typename enable_if< (offset==sizeof(L)), int >::type =0>
void a_function(){}
Run Code Online (Sandbox Code Playgroud)

但不是这个:

template<typename L, size_t offset, typename enable_if< (offset<sizeof(L)), int >::type =0>
class a_class{};

template<typename L, size_t offset, typename enable_if< (offset==sizeof(L)), int >::type =0>
class a_class{};
Run Code Online (Sandbox Code Playgroud)

编译器将第二个类模板视为第一个类的重新定义.

c++ templates sfinae c++11

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

从catch块重新抛出异常时丢失异常类型

今天我在一个catch块中发现了一个错误:

catch (const exception& e){
    // do something
    // throw e; <-- bug!
    throw;    // <-- right thing to do
}
Run Code Online (Sandbox Code Playgroud)

基本上,如果我e 明确地重新抛出异常,我会得到一个新的std::exception重构,实际上该what()方法的消息是默认的std::string,而不是我的自定义构建的消息.

解释是什么?我认为这throw;只是一个简写throw ExceptionJustCaught;.

c++ exception try-catch

14
推荐指数
2
解决办法
2936
查看次数

三元运算符不适用于lambda函数

我正在分配std::function<double()>一个lambda表达式.这个片段有用

if(fn_type==exponential)
    k.*variable = [=,&k](){ return initial*exp(-k.kstep*par); };
else
    k.*variable = [=,&k](){ return initial*pow(k.kstep, par); };
Run Code Online (Sandbox Code Playgroud)

而如果我想使用三元运算符

k.*variable = (fn_type==exponential ? [=,&k](){ return initial*exp(-k.kstep*par); } : [=,&k](){ return initial*pow(k.kstep, par); });
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

error: no match for ternary ‘operator?:’ in <awfully long template error, because this whole thing is in a class defined in a function...>
Run Code Online (Sandbox Code Playgroud)

这是一个gcc bug(我使用的是4.7.2)?否则为什么标准中有这个限制?

c++ lambda conditional-operator c++11

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

std :: runtime_error会复制构造函数中传递的字符串吗?

我想知道这条线是否会创建一个悬空指针:

string arg="derp";
throw std::runtime_error("Unknown argument "+arg);
Run Code Online (Sandbox Code Playgroud)

是否std::runtime_error复制string,或者它存储的参考?

c++ exception

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

标准c ++ 11删除类型的所有指针的方法

有没有办法用一些c ++ 11或至多一个boost库来做到这一点?

#include <iostream>
#include <typeinfo>
using namespace std;

template <typename T> class remove_all_pointers{
public:
    typedef T type;
};

template <typename T> class remove_all_pointers<T*>{
public:
    typedef typename remove_all_pointers<T>::type type;
};

int main(){
    //correctly prints 'i' on gcc
    cout<<typeid(remove_all_pointers<int****>::type).name()<<endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ boost type-traits c++11

11
推荐指数
3
解决办法
1544
查看次数

指向base的指针<=指向派生类的指针?

我想知道C++标准是否保证单个继承使对象"向上"增长,即给定a class Base和a class Derived: public Base,以及指针Derived* ptr,结果dynamic_cast<Base*>(ptr)总是在数值上小于或等于ptr.

c++ inheritance casting

10
推荐指数
2
解决办法
1708
查看次数

有什么理由在64位CPU上使用32位整数进行常见操作吗?

我想知道int在64位程序上继续使用(在x86和x86_64上都是32位)对于没有什么特殊的变量并且实际上不需要跨越2 ^ 64(如迭代计数器)或者最好使用size_t哪个匹配CPU的字大小.

当然,如果你继续使用int你节省了一半的内存,这可能意味着谈论CPU缓存,但我不知道如果在64位机器上每32位数必须在任何使用之前扩展到64位.

编辑:我已经用我的程序进行了一些测试(参见自我回答,我仍然保持janneb的接受,因为它很好).事实证明,性能有了显着提高.

c int 64-bit 32bit-64bit

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