小编Joh*_*mph的帖子

Public alias for non-public type

I wonder if it is valid C++ :

class Test {
    struct PrivateInner {
        PrivateInner(std::string const &str) {
            std::cout << str << "\n";
        }
    };

public:
    using PublicInner = PrivateInner;
};

//Test::PrivateInner priv("Hello world");        // Ok, private so we can't use that
Test::PublicInner publ("Hello World");           // ?, by using public alias we can access private type, is it ok ?
Run Code Online (Sandbox Code Playgroud)

c++ alias access-modifiers

12
推荐指数
1
解决办法
113
查看次数

使用Window恢复功能丢失,在用户首选项中自动保存并保存在NSDocument的模型中

我在主窗口上使用NSOutlineView启动了一个基于文档的小应用程序.我在模型类中使用NSCoding协议将模型保存在文件中,一切都很好.

接下来我想保存用户界面(窗口大小和位置,大纲视图中的展开项目,选择......),我发现将恢复功能添加到Lion.

所以我尝试实现它,我在主窗口和大纲视图中添加了一个自动保存名称,设置了autosaveExpandedItems属性,并在大纲视图的数据源中实现了outlineView:itemForPersistentObject:和outlineView:persistentObjectForItem:方法.

这是有效的,但是当我创建/打开另一个文件然后关闭它时,其他文件的展开项状态将使用已关闭文件的状态进行设置.

这就像应用程序只为所有文档保存一个窗口而不为每个文档保存一个窗口.

我对自动保存和恢复有点失落,它是相同功能还是两个功能完全不同?

我可以用它来保存窗口状态还是需要我将它保存在我的模型中?

谢谢

macos cocoa save nsdocument

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

设置使用initWithNibName初始化的NSViewController的最佳方法:bundle:?

我有一个NSViewController的子类,它从一个nib加载它的视图(使用initWithNibName:bundle:它是该nib的文件所有者).

我需要在加载nib之后进行一些初始化,并且我希望我的代码最兼容:

  • 在ios中:有viewDidLoad方法可以做到这一点
  • 在osx中​​:在雪豹中,没有像viewDidLoad这样的方法,但是在文件的nib所有者上也调用了awakeFromNib

所以我的问题是:

  1. awakeFromNib是否还调用了Lion中文件的nib所有者?
  2. 如果我使用awakeFromNib,我是否需要调用[super awakeFromNib]?(NSViewController是否实现了awakeFromNib?)
  3. 如果答案1为是,这是一个很好的解决方案吗?:
- (void)initAfterNibLoaded {
   ...
}

- (void)viewDidLoad {
   // Code for ios
   [self initAfterNibLoaded];
}

- (void)awakeFromNib {
   // Code for osx

   // Not sure if necessary
   [super awakeFromNib];

   [self initAfterNibLoaded];
}
Run Code Online (Sandbox Code Playgroud)

如果答案1为否,这是一个很好的解决方案吗?:

- (void)viewDidLoad {
   // Initialize after nib loaded
}

#ifndef TARGET_OS_IPHONE
- (void)loadView {
   // Call parent method
   [super loadView];

   // Simulate viewDidLoad method
   [self viewDidLoad];
}
#endif
Run Code Online (Sandbox Code Playgroud)

谢谢

macos initialization nsviewcontroller ios awakefromnib

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

为什么缓存读取未命中比写入未命中更快?

我需要使用另一个数组(readArray)计算一个数组(writeArray),但问题是数组之间的索引映射是不一样的(writeArray的索引x处的值必须使用readArray的索引y处的值计算)所以它不是很缓存友好.

但是我可以选择循环浏览readArray还是顺序浏览writeArray.

所以这是一个简化的代码:

int *readArray = new int[ARRAY_SIZE];       // Array to read
int *writeArray = new int[ARRAY_SIZE];      // Array to write
int *refArray = new int[ARRAY_SIZE];        // Index mapping between read and write, could be also array of pointers instead indexes

// Code not showed here : Initialization of readArray with values, writeArray with zeroes and refArray with random indexes for mapping between readArray and writeArray (values of indexes between 0 and ARRAY_SIZE - 1)

// Version 1: Random read …
Run Code Online (Sandbox Code Playgroud)

c++ performance caching cpu-cache

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

类型别名与类型同名

它是有效的 C++ 吗?

#include <iostream>


class Test {

    struct Inner {
    };

public:
    using Inner = Inner;  // Alias with same name as type
};

int main(int argc, const char * argv[]) {
    static_assert(std::is_pod<Test::Inner>::value, "");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

用 clang 编译得很好,但不能用 GCC/Visual C++ 编译(“内部是私有的...”错误消息)

c++ alias language-lawyer

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

SFINAE不能避免模糊的呼叫

编译此代码:

#include <iostream>


template <int N>
struct TestClass {
    template <int N2, typename std::enable_if<N2 == N, int>::type = 0>
    void doAction() { std::cout << "TestClass::doAction<" << N << ">();\n"; }
};

struct HostClass : public TestClass<1>, public TestClass<2> {
};


int main(int argc, const char * argv[]) {
    HostClass hostClass;

    hostClass.doAction<1>();
    hostClass.doAction<2>();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

导致一个模糊的调用错误,因为它doAction是in TestClass<1>TestClass<2>parent类.

main.cpp:33:15:在多个不同类型的基类中找到成员'doAction'

但是std::enable_if不会禁用这种歧义吗?

编辑:

我认为这种含糊不清的真正原因与此问题相同:

为什么具有相同名称但签名不同的多重继承函数不会被视为重载函数?

可以使用using关键字在答案中显示歧义:

#include <iostream>


template <int N>
struct TestClass { …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance sfinae ambiguous

5
推荐指数
1
解决办法
385
查看次数

使用别名作为范围来获取父成员时不同的编译器行为

此代码在 Clang 和 Visual C++ 上可以正常编译,但在 GCC 上则不行:

#include <iostream>


template <class T>
struct Test {
    Test(T &t) : _t(t) {
    }
    
    void method() {
        std::cout << _t.Internal::_value << "\n";       // Doesn't work on GCC
        std::cout << _t.T::Internal::_value << "\n";    // Work on all compilers
    }

private:
    T &_t;
};

template <class T>
struct Base {
    T _value = 1;
};

template <class T>
struct Child : Base<int> {
    using Internal = Base<int>;
    
    int _value = 2;
};

int main(int …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates dependent-name name-lookup

5
推荐指数
1
解决办法
65
查看次数

如何检查客观C单元测试中的私人成员?

考虑这个课程:

@interface SampleClass : NSObject {
    NSMutableArray *_childs;
}

- (void)addChild:(ChildClass *)child;
- (void)removeChild:(ChildClass *)child;

@end
Run Code Online (Sandbox Code Playgroud)

如果_childs数组包含一个对象而没有添加属性来访问它,我如何测试添加子项时(因为我不希望允许客户端代码访问_childs数组)?

xcode unit-testing objective-c

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

严格的别名规则可以吗?

struct Test {
    void doAction() {}
};

// Create and save into a void*
void *ptr = new Test;

// Real use through a Test*
Test *t = static_cast<Test *>(ptr);
t->doAction();

// Delete
delete static_cast<Test *>(ptr);
Run Code Online (Sandbox Code Playgroud)

ptr仅用于保存对象的地址,并且该地址仅取消引用该对象的真实类型.

所以除非它被解除引用到一个不相关的类型,否则严格的别名规则是可以的呢?

c++ strict-aliasing c++11

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

Use of random in C++

Are these pieces of code equivalent in terms of "randomness" ?

1)

std::vector<int> counts(20);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 19);

for (int i = 0; i < 10000; ++i) {
    ++counts[dis(gen)];
}
Run Code Online (Sandbox Code Playgroud)

2)

std::vector<int> counts(20);
std::random_device rd;
std::mt19937 gen(rd());

for (int i = 0; i < 10000; ++i) {
    std::uniform_int_distribution<> dis(0, 19);
    ++counts[dis(gen)];
}
Run Code Online (Sandbox Code Playgroud)

3)

std::vector<int> counts(20);
std::random_device rd;

for (int i = 0; i < 10000; ++i) {
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, 19);
    ++counts[dis(gen)];
}
Run Code Online (Sandbox Code Playgroud)

4)

std::vector<int> counts(20); …
Run Code Online (Sandbox Code Playgroud)

c++ random

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

为什么在const_iterator中允许移动shared_ptr?

移动shared_ptr会将移动的shared_ptr设置为nullptr,为什么允许它在const_iterator中执行此操作?

std::vector<std::shared_ptr<std::string>> sharedPtrVector;

sharedPtrVector.push_back(std::shared_ptr<std::string>(new std::string("test")));

for (std::vector<std::shared_ptr<std::string>>::const_iterator it = sharedPtrVector.begin(); it != sharedPtrVector.end(); ++it) {
    // Not allowed if const_iterator
    //*it = nullptr;

    // Not allowed if const_iterator
    //*static_cast<std::shared_ptr<std::string> *>(&*it) = nullptr;

    // Allowed even if const_iterator
    std::shared_ptr<std::string> test(std::move(*it));
}
Run Code Online (Sandbox Code Playgroud)

之后,sharedPtrVector处于未定义状态.

iterator const move shared-ptr c++11

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