Singleton ofstram:创建但未写入的文件
Singleton ofstram: Files created but did not be written
我创建了一个单例class来包装一些ofstream
struct OutputFiles {
public:
std::ofstream minus_total, plus_total, HoughMappingMinus_cc, HoughMappingPlus_cc;
template <typename T>
static void init(OutputFilesName<T>& files_name) {
instance_impl(files_name);
}
static OutputFiles& instance() {
return instance_impl();
}
template <typename T>
OutputFiles(OutputFilesName<T>& files_name) :
minus_total(files_name.resut_subdir_name + "/" + files_name.result_file_name_data_M),
plus_total(files_name.resut_subdir_name + "/" + files_name.result_file_name_data_P),
HoughMappingMinus_cc(files_name.resut_subdir_name + "/" + files_name.result_file_name_M_CC),
HoughMappingPlus_cc(files_name.resut_subdir_name + "/" + files_name.result_file_name_P_CC)
{ }
OutputFiles() = default;
OutputFiles(const OutputFiles&) = delete;
OutputFiles& operator=(const OutputFiles&) = delete;
private:
template <typename... Args>
static OutputFiles& instance_impl(Args&&... args) {
static OutputFiles singleton( std::forward<Args&&>(args)... );
return singleton;
}
};
这个 class 必须用 OutputFilesName<T>
class
初始化
template <typename T>
struct OutputFilesName {
OutputFilesName(
const T&& i_resut_subdir_name,
const T&& i_result_file_name_M_CC,
const T&& i_result_file_name_P_CC,
const T&& i_result_file_name_data_M,
const T&& i_result_file_name_data_P
) :
resut_subdir_name(i_result_file_name_M_CC),
result_file_name_M_CC(i_result_file_name_M_CC),
result_file_name_P_CC(i_result_file_name_P_CC),
result_file_name_data_M(i_result_file_name_data_M),
result_file_name_data_P(i_result_file_name_data_P)
{ }
const T resut_subdir_name;
const T result_file_name_M_CC;
const T result_file_name_P_CC;
const T result_file_name_data_M;
const T result_file_name_data_P;
};
我写主要代码调用它:
int main(){
OutputFilesName<std::string> fn{"fa_name","fb_name","aa","bb","cc"};
fs::create_directory(fn.resut_subdir_name);
OutputFiles::init(fn);
OutputFiles::instance().minus_total << "1" << std::endl;
OutputFiles::instance().plus_total << "2" << std::endl;
OutputFiles::instance().HoughMappingMinus_cc << "3"<< std::endl;
OutputFiles::instance().HoughMappingPlus_cc << "4"<< std::endl;
bool open1 = OutputFiles::instance().minus_total.is_open();
bool open2 = OutputFiles::instance().plus_total.is_open();
bool open3 = OutputFiles::instance().HoughMappingMinus_cc.is_open();
bool open4 = OutputFiles::instance().HoughMappingPlus_cc.is_open();
std::cout << open1 << open2 << open3 << open4 << std::endl;
}
结果和完整代码here,
ofstream
好像打不开了
文件和目录已创建,
但是内容是空的
我很困惑。
这是一种管理对象的奇怪方式。
您的 OutputFiles::init(fn)
调用 instance_impl(fn)
,一个函数模板特化。该专业化有一个名为 singleton
的 static
局部变量,使用 ctor args fn
实例化,您 return 对其进行引用。一切顺利。
然后,您 OutputFiles::instance()
得到一个 OutputFiles&
。问题是,该函数调用 instance_impl()
,一个不同的函数模板特化。该特化 也 有一个名为 singleton
的 static
局部变量,默认构造。因此,它的所有 ofstream
成员也都是默认构造的,流式传输给它们什么也做不了。
关键是instance_impl
的两个特化是不同的函数。所以这不是绕过静态初始化的有效方法。
为什么不以正常方式实例化一个 OutputFiles
并将其存储在某个地方?
我创建了一个单例class来包装一些ofstream
struct OutputFiles {
public:
std::ofstream minus_total, plus_total, HoughMappingMinus_cc, HoughMappingPlus_cc;
template <typename T>
static void init(OutputFilesName<T>& files_name) {
instance_impl(files_name);
}
static OutputFiles& instance() {
return instance_impl();
}
template <typename T>
OutputFiles(OutputFilesName<T>& files_name) :
minus_total(files_name.resut_subdir_name + "/" + files_name.result_file_name_data_M),
plus_total(files_name.resut_subdir_name + "/" + files_name.result_file_name_data_P),
HoughMappingMinus_cc(files_name.resut_subdir_name + "/" + files_name.result_file_name_M_CC),
HoughMappingPlus_cc(files_name.resut_subdir_name + "/" + files_name.result_file_name_P_CC)
{ }
OutputFiles() = default;
OutputFiles(const OutputFiles&) = delete;
OutputFiles& operator=(const OutputFiles&) = delete;
private:
template <typename... Args>
static OutputFiles& instance_impl(Args&&... args) {
static OutputFiles singleton( std::forward<Args&&>(args)... );
return singleton;
}
};
这个 class 必须用 OutputFilesName<T>
class
template <typename T>
struct OutputFilesName {
OutputFilesName(
const T&& i_resut_subdir_name,
const T&& i_result_file_name_M_CC,
const T&& i_result_file_name_P_CC,
const T&& i_result_file_name_data_M,
const T&& i_result_file_name_data_P
) :
resut_subdir_name(i_result_file_name_M_CC),
result_file_name_M_CC(i_result_file_name_M_CC),
result_file_name_P_CC(i_result_file_name_P_CC),
result_file_name_data_M(i_result_file_name_data_M),
result_file_name_data_P(i_result_file_name_data_P)
{ }
const T resut_subdir_name;
const T result_file_name_M_CC;
const T result_file_name_P_CC;
const T result_file_name_data_M;
const T result_file_name_data_P;
};
我写主要代码调用它:
int main(){
OutputFilesName<std::string> fn{"fa_name","fb_name","aa","bb","cc"};
fs::create_directory(fn.resut_subdir_name);
OutputFiles::init(fn);
OutputFiles::instance().minus_total << "1" << std::endl;
OutputFiles::instance().plus_total << "2" << std::endl;
OutputFiles::instance().HoughMappingMinus_cc << "3"<< std::endl;
OutputFiles::instance().HoughMappingPlus_cc << "4"<< std::endl;
bool open1 = OutputFiles::instance().minus_total.is_open();
bool open2 = OutputFiles::instance().plus_total.is_open();
bool open3 = OutputFiles::instance().HoughMappingMinus_cc.is_open();
bool open4 = OutputFiles::instance().HoughMappingPlus_cc.is_open();
std::cout << open1 << open2 << open3 << open4 << std::endl;
}
结果和完整代码here,
ofstream
好像打不开了
文件和目录已创建,
但是内容是空的
我很困惑。
这是一种管理对象的奇怪方式。
您的 OutputFiles::init(fn)
调用 instance_impl(fn)
,一个函数模板特化。该专业化有一个名为 singleton
的 static
局部变量,使用 ctor args fn
实例化,您 return 对其进行引用。一切顺利。
然后,您 OutputFiles::instance()
得到一个 OutputFiles&
。问题是,该函数调用 instance_impl()
,一个不同的函数模板特化。该特化 也 有一个名为 singleton
的 static
局部变量,默认构造。因此,它的所有 ofstream
成员也都是默认构造的,流式传输给它们什么也做不了。
关键是instance_impl
的两个特化是不同的函数。所以这不是绕过静态初始化的有效方法。
为什么不以正常方式实例化一个 OutputFiles
并将其存储在某个地方?