小编Vyt*_*ius的帖子

为什么使用'function address == NULL'而不是'false'?

在一些遗留代码中浏览我发现了这样的功能:

static inline bool EmptyFunc()
{
    return (void*) EmptyFunc == NULL;
}
Run Code Online (Sandbox Code Playgroud)

与此有什么不同:

static inline bool EmptyFunc()
{
    return false;
}
Run Code Online (Sandbox Code Playgroud)

这段代码是为了在几个不同的平台上编译而创建的,比如PS2,Wii,PC ......有没有理由使用第一个函数?喜欢更好的优化还是避免一些奇怪的编译器错误行为?

c c++ deobfuscation

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

可以混合复合图案和奇怪的重复模板图案

我有一个复合模式实现,用于GUI组件:

class CObject {
private:

  CObject * m_pParent;  
  CObjectContainer * m_pChildren;

  void private_foo() {
    this->foo();
    //Calls private_foo for each child in container.
    m_pChildren->foo();
  }

public:
  virtual void foo() {
    //empty for base class
  }

  virtual CObject * duplicate() {
    //Do duplication code
    return new CObject(*this);
  }

  virtual CObject * detach() {
    //Remove this object (along with it's children)
    //from current tree.
    m_pParent->RemoveChild(this);
    m_pParent = nullptr;
    return this;
  }
}

class CSpecificObject : public CObject {
public:
  virtual void foo() { …
Run Code Online (Sandbox Code Playgroud)

c++ templates design-patterns composite crtp

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

如何通过引用未知大小的数组来调用函数?

考虑一个有效的代码:

template<size_t size>
void by_numbered_reference(int (&array)[size]);
Run Code Online (Sandbox Code Playgroud)

此函数接受一个数组作为参数,编译器可以使用模板参数推导来推断它的大小.

现在它是有效的(在Apple clang 3.0版上测试)来定义这样的功能:

void by_reference(int (&array)[], int size);
Run Code Online (Sandbox Code Playgroud)

哪个(应该)接受对未知大小数组的引用作为参数.注意到int[]并且int[n]是不同的类型,并且通常是不兼容的.

我发现如何调用此函数的唯一方法是:

int * array;
by_reference(reinterpret_cast<int(&)[]>(*array), array_size);
Run Code Online (Sandbox Code Playgroud)
  1. 为什么语言接受对未知大小数组的引用作为有效函数参数,而没有直接的方法来定义这样的变量?
  2. 是否存在需要此语法的已知用例?
  3. 为什么void by_reference(int (*&array), int size)应该不是用来代替?

c++ arrays syntax

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

CRTP和动态多态编译错误

class A {
    virtual A* foo() = 0;
};

template<class T>
class B : public A {
    virtual T* foo() { return nullptr; }
};

class C : public B<C> {

};
Run Code Online (Sandbox Code Playgroud)

这是混合复合图案和奇怪重复模板图案的可能性的简化实现.我收到以下错误:

Return type of virtual function 'foo' is not covariant with the return type of the function it overrides ('C *' is not derived from 'A *')
Run Code Online (Sandbox Code Playgroud)

在clang 3.0,gcc 4.7和visual studio 2008上测试过.

第一解决方案

class C : public A, public B<C> {}
Run Code Online (Sandbox Code Playgroud)

在Visual Studio中编译一个警告B是已经是一个孩子,并没有下与初始误差铛编译.

另一种解决方法: …

c++ polymorphism standards templates crtp

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

单个模板实例化用于单独的对象

考虑以下:

/* T.h */
template <class T>
void Too() {
    std::cout << "  Template: " << typeid(T).name()
              << " size: " << sizeof(T) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
/* A.h */
extern void FooA();
Run Code Online (Sandbox Code Playgroud)
/* A.cpp */
struct Local {
    int a[2];
}

void FooA() {
    Local l;
    std::cout << "<Foo A>:\n" << "  Real: " << typeid(l).name()
              << " size: " << sizeof(l) << std::endl;
    Too<Local>();
}
Run Code Online (Sandbox Code Playgroud)
/* B.h */
extern void FooB();
Run Code Online (Sandbox Code Playgroud)
/* B.cpp */
struct Local {
    int a[4]; …
Run Code Online (Sandbox Code Playgroud)

c++ templates

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