我需要Linux上的IPC系统.我的要求是:
按重要性排序(粗略).我不需要极端性能,也不会发送大量数据.
我偶然发现了DBus,它看起来像个好人(pecl :: packages :: dbus是让Apache访问DBUS的好机制吗?).但在深入研究DBus文档之前,我想听听一些建议.
它是正确的行为还是这个代码打印1的g ++ 4.5的怪癖?
#include <iostream>
#include <typeinfo>
using namespace std;
int main(){
struct A{};
cout<<(typeid(A)==typeid(const A)&&typeid(A)==typeid(const volatile A)&&typeid(A)==typeid(volatile A));
}
Run Code Online (Sandbox Code Playgroud)
我认为cv限定符的不同类型被威胁为非常不同的类型,即使可以将更少的cv限定类型隐式地转换为更多cv限定类型.
这个简单的代码段使用g ++ 4.7.0生成"函数调用中缺少的标记"警告,如果编译为Cand C++源代码,则都会生成.我认为这是编译器的错误,因为最终NULL
值存在.
#include <unistd.h>
int main() {
execlp("mkdir", "mkdir", "-p", "test", NULL);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我对吗?
按照通过内置函数使用向量指令,这个程序应该编译:
int main(){
double v_sse __attribute__ ((vector_size (16)));
/*
* Should work: "For the convenience in C it is allowed to use a binary vector operation where one operand is a scalar."
*/
v_sse=v_sse+3.4;
/*
* Should work: "Vectors can be subscripted as if the vector were an array with the same number of elements and base type."
*/
double result=v_sse[0];
}
Run Code Online (Sandbox Code Playgroud)
相反,我在两个操作中都遇到错误,抱怨无效的操作数/类型.
我在x86-64系统上编译,所以-msse2是隐式的,我的编译器是4.6.3(也用4.7.0测试,它不起作用).捕获量在哪里?
我有这个模板类:
template<size_t D>
struct A{
double v_sse __attribute__ ((vector_size (8*D)));
A(double val){
//what here?
}
};
Run Code Online (Sandbox Code Playgroud)
v_sse
用副本填写该字段的最佳方法是val
什么?由于我使用向量,我可以使用gcc SSE2内在函数.
在部分专业化的模板参数中使用模板参数时,有没有办法解决标准的限制?我想让它工作的代码是这样的:
template<typename L, size_t offset, typename enable_if< (offset<sizeof(L)), int >::type =0>
class a{};
template<typename L>
class a<L, sizeof(L)-1>{};
Run Code Online (Sandbox Code Playgroud) 为什么这段代码不打印"operator ="?
#include <iostream>
using namespace std;
class A{
public:
template<typename T> void operator=(const T& other){
cout<<"operator="<<endl;
}
};
int main(){
A a;
A b;
a=b;
}
Run Code Online (Sandbox Code Playgroud) 码:
struct A{
const bool const_some_write_once_flag;
A(): const_some_write_once_flag(false) { }
};
struct B: public A{
B(): const_some_write_once_flag(true) { }
};
Run Code Online (Sandbox Code Playgroud)
错误是:类'B'没有任何名为'const_some_write_once_flag'的字段.我相信这是因为在构造函数中B
,正在创建的对象还不是类型A
,因为A
尚未初始化"继承切片" .
我已经尝试了几个没有运气的解决方法,我会在这里省略它们.有没有办法实现我想要做的事情?
有没有办法让这个代码工作,就像用点符号调用静态函数时一样?
struct A{
static void f(){ }
typedef int t;
};
template<typename T> void f(){}
int main(){
A a;
a.f(); //legit
f<a.t>(); //‘a’ cannot appear in a constant-expression, ‘.’ cannot appear in a constant-expression
a.t somevar; //invalid use of ‘A::t’
f<a::t>(); //‘a’ cannot appear in a constant-expression
a::t somevar; //‘a’ is not a class, namespace, or enumeration
}
Run Code Online (Sandbox Code Playgroud)
编辑:伙计们,请在发布之前阅读问题并测试您的代码.这里的要点是不使用A::t
,但"援引" t
通过一个实例的A
,就像你可以用静态方法做.
我对C++处理函数指针和成员函数指针的方式感到困惑,所以我在这个示例代码中提炼了我的怀疑:
#include <iostream>
#include <type_traits>
#include <functional>
#include <typeinfo>
using namespace std;
struct asd{ void f(){ } };
void f(){}
template<typename T> void g(T f){
cout<<"T of g is "<<
(is_pointer<T>::value?"pointer":
(is_function<T>::value?"function":
(is_member_function_pointer<T>::value?"member function pointer":
"something else")))<<" -------- ";
typedef typename remove_pointer<T>::type TlessPointer;
cout<<"T of g less a pointer is "<<
(is_pointer<TlessPointer>::value?"pointer":
(is_function<TlessPointer>::value?"function":
(is_member_function_pointer<TlessPointer>::value?"member function pointer":
"something else")))<<endl;
}
int main(){
cout<<"free function ";
g(f);
cout<<endl<<"(multiple times) dereferenced free function (!!!) ";
g(******f);
cout<<endl<<"member function ";
g(&asd::f);
//this won't compile: …
Run Code Online (Sandbox Code Playgroud) 我是唯一这么认为的人吗?[真正的问题在一秒钟内.]
除了对它的巨大混淆,以及它与互斥或其他锁定机制的混合,当我处理线程安全情况时,我总是不得不放弃任何使用volatile
,因为它根本没有做任何有用的事情.
volatile
"禁止对读/写进行任何重新排序或缓存",但volatile
只要标记单个对象(并将其污染,因为它不再是"普通"对象),这并没有多大用处.
考虑一下:
Thread A Thread B
reads vars
locks mutex (gets access)
locks mutex (waits)
writes some vars
releases mutex
reads vars again
releases mutex
Run Code Online (Sandbox Code Playgroud)
现在,编译器可能希望优化两个线程A的读取,从而将一些结果保存在寄存器中.你说我应该宣布那些变量为volatile
.我说我不想标记所有内容volatile
,因为它volatile
是透明的,我必须复制50%的代码才能支持volatile
类型.您(他)说锁定互斥锁(至少是POSIX互斥锁)是编译器识别并正确管理的东西,要么是对库的调用(编译器无法访问),这可能会改变世界,所以编译器这样的电话会议后不会做任何事情.我说这是依赖于实现的,非常低级别的东西(我不想浏览开发文档以进行日常编程).更糟糕的是,如果由于某种原因,出于某种原因,"外部库"由于任何合法的原因而变得可供编译器访问(可能是作者在必须包含在头文件中的模板中转换函数......无论如何),它会突然改变.
所以,在我看来,volatile
完全没用(和误导),除非是一些非常低级别的东西(设备读取可能,但我不能胜任这样的领域).
更好的方法是明确告诉编译器它必须放弃任何关于任何变量的假设,并且每次后续读取必须是从内存中读取的真实内容.但是我无法想象比调用虚拟外部函数更好的事情,这会产生我之前概述的相同问题.有一种优雅的方式吗?