什么是全局变量的默认存储类?
我在网上搜索时发现,有些网站说它是static.但是,静态意味着内部链接,并且变量在文件范围之外不可用,即它不应该对其他目标文件可用.但是,仍然可以使用声明等方式访问其他文件extern int i.
而且,如果我明确提到static全局变量,那么它在文件范围之外是不可用的.
那么,全局变量的正确默认存储类是什么?
在阅读了一篇关于基于策略的设计并希望自己尝试一些内容的文章后,我花了一些时间重新设计一个记录器类,我曾做过一次基于策略的方法.
一些代码:
template <class Filter, class Formatter, class Outputter>
class LoggerImpl : public LoggerBase {
public:
LoggerImpl(const Filter& filter = Filter(), const Formatter& formatter = Formatter(), const Outputter& outputter = Outputter());
~LoggerImpl();
void log(int channel, int loglevel, const char* msg, va_list list) const;
private:
const Filter mFilter;
const Formatter mFormatter;
const Outputter mOutputter;
};
template <class Filter, class Formatter, class Outputter>
LoggerImpl<Filter, Formatter, Outputter>::LoggerImpl(const Filter& filter, const Formatter& formatter, const Outputter& outputter) :
mFilter(filter), mFormatter(formatter), mOutputter(outputter) {
debuglib::logdispatch::LoggerMgr.addLogger(this);
}
typedef …Run Code Online (Sandbox Code Playgroud) 想要使用外部链接的字符串文字的主要动机是使用字符串文字作为非类型模板参数.
我想象一个带有外部链接的字符串文字,其定义类似于
甲字串文本,其具有在所述前缀的e是一个字串文本与外部连接.
Run Code Online (Sandbox Code Playgroud)template<auto&> struct S{}; void bar() { S<e"foo"> s; }将有相当于的行为
Run Code Online (Sandbox Code Playgroud)template<auto&> struct S{}; constexpr char __foo[] = "foo"; void bar { S<__foo> s; }
有没有理由没有外部链接字符串文字?是否以某种方式添加另一个前缀(如e"Lorem Ipsum")来使字符串文字具有外部链接有害?
注意:已经可以实现外部链接字符串,但这是一种糟糕的做事方式.
#include<boost/metaparse/v1/string.hpp>
template<typename>
struct hack;
template<char... Cs>
struct hack<boost::metaparse::v1::string<Cs...>>
{
static constexpr char arr[] = {Cs..., '\0'};
};
#define E(str) hack<BOOST_METAPARSE_STRING(str)>::arr
template<auto&> struct S{};
S<E("I'm an external linkage string")> s; // compiles
Run Code Online (Sandbox Code Playgroud)
Boost使用python脚本来生成实现BOOST_METAPARSE_STRING,这很糟糕.
该CTRE库能够解析和使用类似语法在编译时验证的正则表达式ctre::match<"REGEX">(text_to_search)。我知道这种语法仅在 C++20 中受支持,这很好,但是无论我尝试什么,我都无法以这种方式使用字符串文字。这是一个非常简单的例子:
// The compiler refuses to pass string literals to STR in this compile time version.
template <char const STR[2]> constexpr int to_int_compile_time()
{
return STR[0] - '0';
}
// It has no problems passing the string literal to str in this version.
int to_int_runtime(char const str[2])
{
return str[0] - '0';
}
Run Code Online (Sandbox Code Playgroud)
调用to_int_runtime("0")工作正常,但to_int_compile_time<"0">()抱怨字符串文字不能用于此模板参数。应该如何to_int_compile_time编写才能将字符串文字传递到 char 数组模板参数中?
我有一些例外来自std::exception或std::runtime_error.唯一的方法是构造函数explicit MyExceptionX(const char *text = "") : std::exception(text) {}.有没有办法在不使用宏的情况下简化这些代码?
class MyException1: public std::exception
{
public:
explicit MyException1(const char *text = "") : std::exception(text) {}
};
class MyException2: public std::exception
{
public:
explicit MyException2(const char *text = "") : std::exception(text) {}
};
class MyException3: public std::exception
{
public:
explicit MyException3(const char *text = "") : std::exception(text) {}
};
//...
Run Code Online (Sandbox Code Playgroud)