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) 我在主窗口上使用NSOutlineView启动了一个基于文档的小应用程序.我在模型类中使用NSCoding协议将模型保存在文件中,一切都很好.
接下来我想保存用户界面(窗口大小和位置,大纲视图中的展开项目,选择......),我发现将恢复功能添加到Lion.
所以我尝试实现它,我在主窗口和大纲视图中添加了一个自动保存名称,设置了autosaveExpandedItems属性,并在大纲视图的数据源中实现了outlineView:itemForPersistentObject:和outlineView:persistentObjectForItem:方法.
这是有效的,但是当我创建/打开另一个文件然后关闭它时,其他文件的展开项状态将使用已关闭文件的状态进行设置.
这就像应用程序只为所有文档保存一个窗口而不为每个文档保存一个窗口.
我对自动保存和恢复有点失落,它是相同功能还是两个功能完全不同?
我可以用它来保存窗口状态还是需要我将它保存在我的模型中?
谢谢
我有一个NSViewController的子类,它从一个nib加载它的视图(使用initWithNibName:bundle:它是该nib的文件所有者).
我需要在加载nib之后进行一些初始化,并且我希望我的代码最兼容:
所以我的问题是:
Run Code Online (Sandbox Code Playgroud)- (void)initAfterNibLoaded { ... } - (void)viewDidLoad { // Code for ios [self initAfterNibLoaded]; } - (void)awakeFromNib { // Code for osx // Not sure if necessary [super awakeFromNib]; [self initAfterNibLoaded]; }
如果答案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)
谢谢
我需要使用另一个数组(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++ 吗?
#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++ 编译(“内部是私有的...”错误消息)
编译此代码:
#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) 此代码在 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) 考虑这个课程:
@interface SampleClass : NSObject {
NSMutableArray *_childs;
}
- (void)addChild:(ChildClass *)child;
- (void)removeChild:(ChildClass *)child;
@end
Run Code Online (Sandbox Code Playgroud)
如果_childs数组包含一个对象而没有添加属性来访问它,我如何测试添加子项时(因为我不希望允许客户端代码访问_childs数组)?
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仅用于保存对象的地址,并且该地址仅取消引用该对象的真实类型.
所以除非它被解除引用到一个不相关的类型,否则严格的别名规则是可以的呢?
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) 移动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处于未定义状态.
c++ ×7
alias ×2
c++11 ×2
inheritance ×2
macos ×2
ambiguous ×1
awakefromnib ×1
caching ×1
cocoa ×1
const ×1
cpu-cache ×1
ios ×1
iterator ×1
move ×1
name-lookup ×1
nsdocument ×1
objective-c ×1
performance ×1
random ×1
save ×1
sfinae ×1
shared-ptr ×1
templates ×1
unit-testing ×1
xcode ×1