我用来LD_LIBRARY_PATH
为应用程序设置某个用户库的路径.但是如果我在这个应用程序上设置功能
sudo setcap CAP_NET_BIND_SERVICE=eip myapplication
Run Code Online (Sandbox Code Playgroud)
然后LD_LIBRARY_PATH
似乎被忽略了.当我启动程序时,Linux抱怨它无法找到某个共享库.
我猜这里有一些保护措施,以防止具有扩展权限的应用程序被劫持.有解决方法吗?
在成员函数中编写lambda函数时,有没有办法按值捕获封闭类的字段?默认的catch-all =
不起作用,因为当我在lambda中引用变量时,我得到的是从捕获的this指针中取消引用,以及在捕获列表中显式命名变量,因为我得到两个编译错误:capture of non-variable <name>
,和‘this’ was not captured for this lambda function
考虑以下代码:
#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 ++没有抱怨 …
有人可以解释为什么编译器只接受这个代码
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)
编译器将第二个类模板视为第一个类的重新定义.
今天我在一个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;
.
我正在分配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)?否则为什么标准中有这个限制?
我想知道这条线是否会创建一个悬空指针:
string arg="derp";
throw std::runtime_error("Unknown argument "+arg);
Run Code Online (Sandbox Code Playgroud)
是否std::runtime_error
复制string
,或者它存储的参考?
有没有办法用一些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++标准是否保证单个继承使对象"向上"增长,即给定a class Base
和a class Derived: public Base
,以及指针Derived* ptr
,结果dynamic_cast<Base*>(ptr)
总是在数值上小于或等于ptr
.
我想知道int
在64位程序上继续使用(在x86和x86_64上都是32位)对于没有什么特殊的变量并且实际上不需要跨越2 ^ 64(如迭代计数器)或者最好使用size_t
哪个匹配CPU的字大小.
当然,如果你继续使用int
你节省了一半的内存,这可能意味着谈论CPU缓存,但我不知道如果在64位机器上每32位数必须在任何使用之前扩展到64位.
编辑:我已经用我的程序进行了一些测试(参见自我回答,我仍然保持janneb的接受,因为它很好).事实证明,性能有了显着提高.