我参与了一个涉及复杂布尔逻辑的项目.这种复杂性使代码非常有效,但遗憾的是难以阅读.
因此,我们将逻辑排列如下,这使得更容易在复合语句中查看组,并且还可以向逻辑的某些部分添加注释.
(这段代码不是来自项目的真实代码,真正的逻辑更复杂)
if (
//comments here!
angle.angle < kQuiteLow
&& (
previousAngle.angle > kQuiteHigh
|| previousAngle.time == kUnknownTime
)
//comments in here too!
&& pairedAngle.angle < kQuiteLow
&& (
//and here
previousPairedAngle.angle > kQuiteHigh
|| previousPairedAngle.time == kUnknownTime
)
)
Run Code Online (Sandbox Code Playgroud)
你有没有见过这在其他任何地方完成?
关于如何布置非常复杂的布尔逻辑,是否有任何约定或样式指南建议?
我有一个3D矢量类.私有变量定义如下:
union { struct { double x; double y; double z; }; double data[3]; };
在实现operator ==时哪个更快?
return this->x == v.x && this->y == v.y && this->z == v.z;
要么
return memcmp(this->data, v.data) == 0;
所以我一直在考虑PIMPL和堆栈分配.我一直在写一个库,并决定使用PIMPL隐藏该类的私有成员.这意味着我会有一个像这样声明的类
class Foo {
private:
class Handle;
std::tr1::shared_ptr<Handle> handle;
public:
Foo();
};
Run Code Online (Sandbox Code Playgroud)
这很直接.但是然后在构造函数中你这样做
Foo::Foo() : handle(new Handle()) {}
Run Code Online (Sandbox Code Playgroud)
因此,当使用我的库的人在堆栈上创建Foo时,他们实际上是在进行堆分配.这是使用PIMPL时必须要考虑的权衡吗?我想在构造函数旁边发出警告释放文档:"警告:这会导致堆分配"或者其他一些问题.
我的另一个想法是将所有暴露给实现的类作为纯虚拟接口和一大堆静态工厂方法返回智能指针.这也意味着堆分配,但没有技巧.
有什么想法或建议吗?我是否过度考虑使用我的图书馆的程序员?
我是否需要Alpha通道才能在OpenGL中使用透明度?我可以使用glBlendFunc或其他任何方式使黑色或白色透明/不可见吗?如果有,该怎么办?
我想使用std :: tm()作为std :: map-container的键.但是当我尝试编译它时,我得到了很多(10)错误.
例如:
1.
错误C2784:'bool std :: operator <(const std :: basic_string <_Elem,_Traits,_Alloc>&,const _Elem*)':无法推断'const std :: basic_string <_Elem,_Traits,_Alloc>的模板参数&'from'const tm'c:\ program files(x86)\ microsoft visual studio 10.0\vc\include\xfunctional 125
2.
错误C2784:'bool std :: operator <(const _Elem*,const std :: basic_string <_Elem,_Traits,_Alloc>&)':无法从'const tm'c:\中推断'const _Elem*'的模板参数程序文件(x86)\ microsoft visual studio 10.0\vc\include\xfunctional 125
3.
错误C2784:'bool std :: operator <(const std :: vector <_Ty,_Ax>&,const std :: vector <_Ty,_Ax>&)':无法推断'const std :: vector <的模板参数_Ty,_Ax>&'from'const tm'c:\ program files(x86)\ microsoft visual studio 10.0\vc\include\xfunctional 125
这一切是否意味着,我"只是"必须创建一个比较两个std :: tm的函数对象,因为没有为此定义的标准比较?还是有另一招?(或者对我来说甚至可能不可能?^^)
码:
#include <map>
#include …
Run Code Online (Sandbox Code Playgroud) 我的想法是任何一个对象都应该存在于 Singleton 类的所有子类中。我一直在尝试的代码和结果矩阵如下所示。在子类的情况下,矩阵似乎工作得很好。我是不是走错路了?它是否了解父类对象和子类对象的情况会发生什么?
class Singleton(object):
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(
cls, *args, **kwargs)
return cls._instance
class A(Singleton):
def __new__(cls, *args, **kwargs):
super(A, cls).__new__(cls, *args, **kwargs)
class B(Singleton):
def __new__(cls, *args, **kwargs):
super(B, cls).__new__(cls, *args, **kwargs)
class C(B):
def __new__(cls, *args, **kwargs):
super(B, cls).__new__(cls, *args, **kwargs)
if __name__ == '__main__':
s1=Singleton()
s2=Singleton()
if(id(s1)==id(s2)):
print "Same"
else:
print "Different"
'''
I got a result matrix for s1 and s2
|************ s2 **************************|
s1 …
Run Code Online (Sandbox Code Playgroud) 我有一个简单的程序:
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
string read0() {
int length = 4;
char *cstr = new char[length];
string str(cstr);
delete[] cstr;
return str;
}
string read1() {
int length = 4;
char cstr[length];
memset(cstr, '-', 4);
string str(cstr);
return str;
}
string read2() {
const char* cstr = "abcd";
string str(cstr);
return str;
}
Run Code Online (Sandbox Code Playgroud)
在上面的所有3个函数中,用于构造字符串,它们call basic_string( const CharT* s, const Allocator& alloc = Allocator()
.当我使用valgrind/massif检查堆使用情况时,函数read0仅使用4个字节(from new
),但read1和read2都使用29个字节.
这是地块的一些细节输出:
对于read0:
16.67%(4B)(堆分配函数)malloc/new/new [], - all-fns等
- …
我正在尝试在 Ubuntu Trusty 系统上使用 g++ 4.9.1 链接一个非常简单的 GLES2 和 EGL 程序。我正在使用台面库。
我收到 EGL 函数的链接器错误:
test.cpp:(.text+0x342): undefined reference to `eglGetDisplay'
test.cpp:(.text+0x389): undefined reference to `eglInitialize'
test.cpp:(.text+0x40f): undefined reference to `eglCreateContext'
test.cpp:(.text+0x458): undefined reference to `eglCreatePbufferSurface'
test.cpp:(.text+0x49e): undefined reference to `eglMakeCurrent'
Run Code Online (Sandbox Code Playgroud)
我正在编译 test.cpp
g++ -std=c++0x -Wall -Werror -lEGL -lGLESv2 -o test test.cpp
Run Code Online (Sandbox Code Playgroud)
我试过切换库的顺序,这有时很重要,但我遇到了同样的问题。这里有我缺少的图书馆吗?
我已经运行readelf -Ws /usr/lib/x86_64-linux-gnu/mesa-egl/libEGL.so
并定义了所有必需的功能。
我是斯卡拉的新手.在学习Actor时,我试图将其扩展为保存一行def:
import scala.actors.Actor
import Actor._
class Actoo(actoo: =>Unit) extends Actor {
def act() {actoo}
}
object run extends Application {
/*
// this one runs well
val a = new Actor {
def act() {
receive { case 1 => println("1") }
}
}
*/
val a = new Actoo {
receive { case 1 => println("1") }
}
a.start
a ! 1
}
Run Code Online (Sandbox Code Playgroud)
然后异常跟踪如下所示:
java.lang.AssertionError: assertion failed: receive from channel belonging to other actor
at scala.Predef$.assert(Predef.scala:92)
at scala.actors.Actor$class.receive(Actor.scala:424)
at …
Run Code Online (Sandbox Code Playgroud) 在[basic.scope.declarative] p4中,有人读
给定一个声明区域中的一组声明,每个声明都指定相同的非限定名称, - (4.1)它们都应引用同一个实体......
天真的阅读可能意味着以下代码可能有效,因为"两个声明都引用同一个实体":
int x;
int x;
Run Code Online (Sandbox Code Playgroud)
然后可能会记住一个定义规则[basic.def.odr] p1.上述推理可能仅适用于不是定义的声明.区别在[basic.def] p2中详细说明.例如,以下代码肯定是有效的:
extern int x;
extern int x;
Run Code Online (Sandbox Code Playgroud)
[basic.def] p2中的最后一个示例表明以下代码应该有效,但它不能编译(使用MSVC2015).
struct B
{
int y;
};
struct D : B
{
using B::y;
using B::y;
};
Run Code Online (Sandbox Code Playgroud)
问题出在哪儿?
错误消息是
'B :: y'的using声明不能与'B :: y'的现有using声明共存
c++ ×5
actor ×1
c++11 ×1
coding-style ×1
comparison ×1
egl ×1
g++ ×1
heapalloc ×1
inheritance ×1
key ×1
map ×1
opengl ×1
pimpl-idiom ×1
python ×1
scala ×1
singleton ×1
stackalloc ×1