哪种转换更好,有什么区别?
class Base
{};
class Derived : public Base, public std::enable_shared_from_this<Derived>
{};
int main(int argc, const char * argv[])
{
std::shared_ptr<Base> ptr1 = std::dynamic_pointer_cast<Base>(std::shared_ptr<Derived>(new Derived())); // version 1
std::shared_ptr<Base> ptr2 = std::shared_ptr<Derived>(new Derived()); // version 2
return 0;
}
Run Code Online (Sandbox Code Playgroud) 当第二个示例只调用基类中的析构函数时,为什么在使用std :: shared_ptr deallocation从基类和派生类调用析构函数?
class Base
{
public:
~Base()
{
std::cout << "Base destructor" << std::endl;
}
};
class Derived : public Base
{
public:
~Derived()
{
std::cout << "Derived destructor" << std::endl;
}
};
void virtual_destructor()
{
{
std::cout << "--------------------" << std::endl;
std::shared_ptr<Base> sharedA(new Derived);
}
std::cout << "--------------------" << std::endl;
Base * a = new Derived;
delete a;
}
Run Code Online (Sandbox Code Playgroud)
输出:
--------------------
Derived destructor
Base destructor
--------------------
Base destructor
Run Code Online (Sandbox Code Playgroud)
在这两种情况下我都期待相同的行为.
我正在开发采用截图的静态库,从OpenGL应用程序中获取它们需要特殊处理.
当客户端应用程序链接到我的静态库时,它必须添加我的库使用的框架,例如采用OpenGL截图,即使客户端应用程序不使用OpenGL,它也必须链接OpenGLES.framework,这是不好的.我试图检查库是否客户端已与OpenGLES.framework链接并动态启用从OpenGL截取屏幕截图.
问题是当我尝试使用C函数时出现编译错误:
if(&glReadPixels != NULL) {
glReadPixels(0, 0, size.width, size.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我可以检查方法是否存在,但是如何调用它以不导致链接器错误?当我用我的库编译客户端时,我得到了这个:
Undefined symbols for architecture i386:
"_glReadPixels", referenced from:
+[TakeScreenshotUtil takeOpenGLScreenshotWithContext:layerSize:] in libScr-iOS.a(TakeScreenshotUtil.o)
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用
__attribute__ ((weak))
Run Code Online (Sandbox Code Playgroud)
但它不起作用(不会改变任何东西).
为什么这段代码会崩溃?
#include <iostream>
#include <functional>
int main(int argc, const char * argv[])
{
std::function<void(int)> function = [](int)
{
};
auto binding = std::bind(function, 10);
std::function<void()> jobFunctor = binding; // crashes here with EXC_BAD_ACCESS
return 0;
}
Run Code Online (Sandbox Code Playgroud)
转换绑定结果时,构造函数中jobFunctor存在无限堆栈递归std::function.
我正在运行Mac OS X 10.8.5,我使用libc ++编译此代码与Xcode 5.0.2,编译器版本:
LO50F-04-198BX:$ clang++ --version
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写函数,func以便编译器可以推导出模板参数,它在我传入时有效std::function,但不能与lambdas一起使用:
template<typename TResult>
TResult func(std::function<TResult()> f)
{
return TResult();
}
int main()
{
// Visual Studio 2013
int result = func([]() { // error: 'TResult func(std::function<TResult(void)>)' : could not deduce template argument for 'std::function<TResult(void)>' from 'main::<lambda_d9d7854806072a2cb711f56185602ccb>'
return 100;
});
std::function<int()> testFunc = []() {
return 100;
};
int result2 = func(testFunc); // this works
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是否有可能推导出lambda的模板参数,以便该行编译?而不是写func<int>([](){ return 100; });我想写func([](){ return 100; });
我想编写一个将一个闭包作为参数的函数,该函数将另一个闭包作为参数,如下所示:
fn test<F: Fn(G), G: Fn()>(f: F) {
let print = || {
println!("hello");
};
f(print);
}
fn main() {
test(|print| {
print();
})
}
Run Code Online (Sandbox Code Playgroud)
但这给出了这个错误:
error[E0308]: mismatched types
--> src/main.rs:5:7
|
1 | fn test<F: Fn(G), G: Fn()>(f: F) {
| - expected this type parameter
2 | let print = || {
| -- the found closure
...
5 | f(print);
| - ^^^^^ expected type parameter `G`, found closure
| |
| arguments to this function are …Run Code Online (Sandbox Code Playgroud) 我使用CATiledLayer作为我的UIView的支持层,我把它放在UIScrollView中.在我的视图的init方法中,我正在创建绘制简单线条的CGPathRef对象.当我尝试在drawLayer:inContext中绘制此路径时,当我滚动/缩放时,它偶尔会与EXEC_BAD_ACCESS(很少)崩溃.
代码很简单,我只使用标准的CG*函数:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
tiledLayer.levelsOfDetail = 10;
tiledLayer.levelsOfDetailBias = 5;
tiledLayer.tileSize = CGSizeMake(512.0, 512.0);
CGMutablePathRef mutablePath = CGPathCreateMutable();
CGPathMoveToPoint(mutablePath, nil, 0, 0);
CGPathAddLineToPoint(mutablePath, nil, 700, 700);
path = CGPathCreateCopy(mutablePath);
CGPathRelease(mutablePath);
}
return self;
}
+ (Class) layerClass {
return [CATiledLayer class];
}
- (void) drawRect:(CGRect)rect {
}
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
CGContextFillRect(ctx, self.bounds);
CGContextSetLineWidth(ctx, 5);
CGContextAddPath(ctx, path);
CGContextDrawPath(ctx, kCGPathStroke); …Run Code Online (Sandbox Code Playgroud) c++ ×4
c++11 ×2
ios ×2
iphone ×2
lambda ×2
shared-ptr ×2
catiledlayer ×1
cgpath ×1
ipad ×1
objective-c ×1
rust ×1