我为SDL2库方法创建了包装函子,以使用自定义删除器返回智能指针。对于unqiue_ptr(类Image)来说似乎工作正常,但是对于在构建过程中返回shared_ptr(类Window)的类给出以下错误:
'<function-style-cast>': cannot convert from 'initializer list' to 'std::shared_ptr<SDL_Window>'
此处的SDL_CreateWindow
返回原始SDL_Window*
,而IMG_Load
返回原始SDL_Surface*
。
我曾尝试将Deleter
移到公开位置,并取消了Window类的复制限制,但是仍然失败,并出现相同的错误。另外,如果我只是从Window的函数强制转换中返回nullptr
,那么它的构建就很好。因此,问题似乎出在shared_ptr本身的创建上。使我感到困惑的是为什么它可以与unique_ptr
一起使用但不能与shared_ptr
一起使用。
#pragma once
#include <memory>
#include <SDL.h>
#include "Uncopyable.h"
// fails during build with error: '<function-style-cast>':
// cannot convert from 'initializer list' to 'std::shared_ptr<SDL_Window>'
class Window:private Uncopyable {
private:
public:
class Deleter {
void operator()(SDL_Window *window) {
SDL_DestroyWindow(window);
}
};
static const int SCREEN_WIDTH = 800;
static const int SCREEN_HEIGHT = 600;
std::shared_ptr<SDL_Window> operator()() const {
return std::shared_ptr<SDL_Window>(
SDL_CreateWindow("SDL Tutorial",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN),
Deleter());
}
};
#pragma once
#include <memory>
#include <string>
#include <SDL.h>
#include <SDL_image.h>
#include "Uncopyable.h"
// builds fine
class Image: private Uncopyable {
public:
class Deleter{
void operator()(SDL_Surface *image) {
SDL_FreeSurface(image);
}
};
std::unique_ptr<SDL_Surface, Deleter> operator()(const std::string &path) const {
return std::unique_ptr<SDL_Surface, Deleter>(
IMG_Load(path.c_str()),
Deleter());
}
};
预期结果:窗口类应该像图像类一样正确构建
实际结果:Window类由于上面给出的错误而失败,而Image类构建良好
更新:通过将shared_ptr创建逻辑移动到简单函数来进一步缩小范围,我发现删除自定义Deleter()
会删除构建错误。因此,这似乎是罪魁祸首。但是我需要Deleter,以及为什么相同的构造可以使用unique_ptr在Image上正常工作。
答案 0 :(得分:1)
我已经简化了您的示例:
#include <memory>
// Stub these out since we don't have them available and they don't really matter
// for the substance of the question.
struct SDL_Window {};
void SDL_DestroyWindow( SDL_Window* win ) { delete win; }
SDL_Window* SDL_CreateWindow() { return new SDL_Window{}; }
// fails during build with error: '<function-style-cast>':
// cannot convert from 'initializer list' to 'std::shared_ptr<SDL_Window>'
class Window {
public:
class Deleter {
void operator()(SDL_Window *window) {
SDL_DestroyWindow(window);
}
};
std::shared_ptr<SDL_Window> operator()() const {
return std::shared_ptr<SDL_Window>(
SDL_CreateWindow(),
Deleter());
}
};
int main()
{
auto win = Window();
auto sp = win();
}
现在问题更加明显:
/usr/local/include/c++/8.2.0/bits/shared_ptr_base.h:642:11: error:
'void Window::Deleter::operator()(SDL_Window*)' is private within this context
__d(__p); // Call _Deleter on __p.
~~~^~~~~
main.cpp:16:14: note: declared private here
void operator()(SDL_Window *window) {
看到它在 Coliru 上实时失败。
如果将public
添加到删除器类或将其设为结构,它将起作用。但是,您也可以跳过该类,如果需要完成所有工作,则直接直接传递delete函数(或者,如果它有点复杂,则使用lambda):
std::shared_ptr<SDL_Window> operator()() const {
return std::shared_ptr<SDL_Window>(
SDL_CreateWindow(),
SDL_DestroyWindow);
}
// Or with a lambda if it's more complicated (here also using a factory func)
static std::shared_ptr<SDL_Window> Create() {
return std::shared_ptr<SDL_Window>(
SDL_CreateWindow(),
[] (auto win) {
UnregisterMyWindow( win );
SDL_DestroyWindow( win );
});
}
看到它在 Coliru 上实时运行。
此外,像这样使用Window::operator()
也是有疑问的。我建议您改用非成员或静态成员工厂函数来制作Windows。