C++ 仅使用宏在标题中定义静态变量
我的目标是能够在标题中单独创建一个静态变量,并使用宏在C++ 仅使用宏在标题中定义静态变量,c++,c++11,static,macros,C++,C++11,Static,Macros,我的目标是能够在标题中单独创建一个静态变量,并使用宏在.cpp文件中使用我提供的值对其进行初始化。它应该是这样的: struct UserDefaults { STATIC(bool, isFullscreen, true) STATIC(bool, isBorderless, false) STATIC(std::string, profileName, "") } 这相当于: // .hpp file struct UserDefaults { stati
.cpp
文件中使用我提供的值对其进行初始化。它应该是这样的:
struct UserDefaults {
STATIC(bool, isFullscreen, true)
STATIC(bool, isBorderless, false)
STATIC(std::string, profileName, "")
}
这相当于:
// .hpp file
struct UserDefaults {
static bool isFullscreen;
static bool isBorderless;
static std::string profileName;
}
// .cpp file
bool UserDefaults::isFullscreen = true;
bool UserDefaults::isBorderless= false;
std::string UserDefaults::profileName = "";
我已经看过了,但我无法将Pesche的解决方案应用到我的案例中。为什么要使用宏?这是C++。
#include <iostream>
#define STATIC(type, name, value) \
static type& name() { static type ret = value; return ret; }
struct UserDefaults
{
STATIC(bool, isFullscreen, true)
STATIC(bool, isBorderless, false)
STATIC(std::string, profileName, "")
};
int main()
{
UserDefaults ud;
std::cout << ud.isFullscreen() << " " << ud.isBorderless() << " " << ud.profileName() << std::endl;
}
#include <string>
#include <iostream>
template<class Type, Type(*init)()>
struct static_thing
{
using value_type = Type;
operator value_type&() const {
return get();
}
static value_type& get() {
static value_type _ { init() };
return _;
}
/*
* add whatever operations you need
*/
template<class Source>
value_type& operator=(Source&& value) {
get() = std::forward<Source>(value);
}
friend auto operator<<(std::ostream& os, static_thing const& st) -> std::ostream&
{
return os << st.get();
}
};
inline bool make_true() { return true; }
inline bool make_false() { return true; }
inline std::string empty_string() { return std::string(); }
struct UserDefaults
{
static_thing<bool, make_true> isFullscreen;
static_thing<bool, make_false> isBorderless;
static_thing<std::string, empty_string> profileName;
};
int main()
{
auto defs = UserDefaults();
defs.profileName = "foo";
std::cout << defs.profileName << " " << defs.isFullscreen << std::endl;
}
#包括
#包括
模板
结构静态对象
{
使用值\类型=类型;
运算符值类型&()常量{
返回get();
}
静态值\u type&get(){
静态值\u类型{init()};
返回;;
}
/*
*添加您需要的任何操作
*/
模板
值\类型和运算符=(源和值){
get()=std::forward(值);
}
朋友自动操作符< P>为什么使用宏?这是C++。
#include <string>
#include <iostream>
template<class Type, Type(*init)()>
struct static_thing
{
using value_type = Type;
operator value_type&() const {
return get();
}
static value_type& get() {
static value_type _ { init() };
return _;
}
/*
* add whatever operations you need
*/
template<class Source>
value_type& operator=(Source&& value) {
get() = std::forward<Source>(value);
}
friend auto operator<<(std::ostream& os, static_thing const& st) -> std::ostream&
{
return os << st.get();
}
};
inline bool make_true() { return true; }
inline bool make_false() { return true; }
inline std::string empty_string() { return std::string(); }
struct UserDefaults
{
static_thing<bool, make_true> isFullscreen;
static_thing<bool, make_false> isBorderless;
static_thing<std::string, empty_string> profileName;
};
int main()
{
auto defs = UserDefaults();
defs.profileName = "foo";
std::cout << defs.profileName << " " << defs.isFullscreen << std::endl;
}
#包括
#包括
模板
结构静态对象
{
使用值\类型=类型;
运算符值类型&()常量{
返回get();
}
静态值\u type&get(){
静态值\u类型{init()};
返回;;
}
/*
*添加您需要的任何操作
*/
模板
值\类型和运算符=(源和值){
get()=std::forward(值);
}
friend auto Operator单个宏不可能实现这种分离。您需要使用编译前运行的预处理脚本来分析源代码并生成此类代码。似乎我在几年前就听到过类似的讨论,最终结果是决定使用静态成员函数。。。该函数有一个返回引用的单成员变量…模板也可能是一种方法…@RemyLebeau我知道没有单一的宏解决方案。我试图使用我在创建宏以进行初始化、读取和写入时发布的问题中的模板来使用该解决方案。宏只会生成无法删除的模糊代码难看且不可读。难道你不能再点击键盘20次吗?一个宏不可能实现这种分离。你需要使用编译前运行的预处理脚本来分析源代码并生成这样的代码。似乎我在几年前听到过一个类似于此的主题的讨论,以及最终结果是否决定使用静态成员函数…函数有一个返回引用的单个成员变量…模板也可能是一种方法…@RemyLebeau我知道没有单一的宏解决方案。我尝试使用我发布的问题中的模板来使用解决方案,该问题与创建宏进行初始化、读取和写入有关ing.macros只会生成不可调试且不可读的模糊代码。你就不能再点击键盘20次吗?为什么isFullscreen
int
不是bool
?我使用的正是你提出的方法,但问题是你不能写入该值,只能从中读取。我需要它是一个静态变量。@Marošbeťko:仔细看!那些静态函数返回对其中静态变量的引用,这样你就可以分配给它们了。为什么isFullscreen
int
不是bool
?我使用的正是你提出的方法,但问题是你不能写入该值,只能从中读取。我需要它是一个静态变量能行。@MaroťBeťko:仔细看!这些静态函数返回对其中静态变量的引用,因此您可以为它们赋值。