简体   繁体   English

C++:只有头文件的项目,静态常量非整型

[英]C++: Header-only project, static const non-integral

I have a header-only project .我有一个仅限标题的项目 Inside it I have a class.在里面我有一堂课。 Inside it (or anywhere else actually) I would like to have constant data (enum values to string and vice-verse).在它里面(或其他任何地方),我想要常量数据(字符串的枚举值,反之亦然)。 This problem seems a lot harder that I expected.这个问题似乎比我预期的要困难得多。

typedef boost::bimap<MyEnum,std::string> Data;

What I tried and did not work:我尝试过但没有用的:

  1. static Data const s_data = _initData(); : Error is like: only static const integral data members can be initialized within a class . : 错误就像: only static const integral data members can be initialized within a class

  2. static Data const * const s_pData = _initData(); : The _initData() function had a static local variable (which became filled on first call), and returned the address of it. : _initData()函数有一个静态局部变量(在第一次调用时被填充),并返回它的地址。 Did not work with the same reason as above.没有与上述相同的原因工作。

What I tried and worked, but I consider it ugly:我尝试和工作的东西,但我认为它很丑陋:

class Ugly {
public:
    static MyEnum lookupByName(std::string s)
    {
        MyEnum ret;
        lookup(ret,s,true);
        return ret;
    }
    static String lookupByEnum(MyEnum e)
    {
        std::string ret;
        lookup(e,ret,false);
        return ret;
    }
    static void lookup(MyEnum &e, std::string &s, bool etos)
    {
        static Data s_data = _fill();
        if(etos)
            s = /* ... */;
        else
            e = /* ... */;
    }
    static Data _fill(){ /* ... */ };
};

Ideas?想法?

The simpler is更简单的是

static T& global_t()
{ static T z = initializer; return z; }

global_t() can be used whereve a T value is required. global_t()可用于需要 T 值的地方。

NOTE : In answer to rioki comment, we must also specify the function as inline if it is at global or namespace level (to avoid the "multiple instances" problem towards the linker).注意:为了回答 rioki 评论,我们还必须将函数指定为inline函数,如果它是在全局或命名空间级别(以避免链接器的“多实例”问题)。

The inline keyword is not necessary if the function is a template or is a class member-function (for which the inline definition is by default)如果函数是模板或类成员函数(默认为内联定义),则不需要 inline 关键字

If the static T instantiation must be shared among different OS modules (read: DLLs) rioki is perfectly right, but -at that point- a header-only library makes no more sense.如果static T实例化必须在不同的 OS 模块(读取:DLL)之间共享,rioki 是完全正确的,但是 - 在这一点上 - 仅头文件库没有任何意义。


Starting from C++17, the inline specifier can also be used on variables.从 C++17 开始, inline说明符也可以用于变量。

So, from C++17 onward, you can just write所以,从 C++17 开始,你可以只写

inline T global_object = initializer;

You can also use inline for static members of function, to provide inline initialization, like您还可以对函数的静态成员使用内联,以提供内联初始化,例如

class Class
{
    static inline Type static_object_name = initializer;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 初始化类的静态const非整数数据成员 - Initialize a static const non-integral data member of a class 用私有成员初始化静态const非整数类成员 - Initialise static const, non-integral, class member with a private member 静态const(非整数)成员初始化语法背后的基本原理? - Rationale behind static const (non-integral) member initialization syntax? C ++非整体模板Const初始化,需要在ClassName之前使用init-declarator - C++ Non-Integral template Const Initialization expected init-declarator before ClassName C++ 必须定义为静态 constexpr double - 不能在类中初始化静态非整型变量 - C++ must define as static constexpr double - cant initialize static non-integral variable inside Class 非整数常量在C ++中如何工作? - How do non-integral constants work in C++? c ++ map stl错误:数组&#39;apn2policy&#39;的大小具有非整数类型&#39;const char [13]&#39; - c++ map stl error : size of array 'apn2policy' has non-integral type 'const char [13]' Visual Studio 2013 CTP是否支持非整数类型的类内静态const初始化程序? - Does Visual Studio 2013 CTP support in-class static const initializers for non-integral types? 为什么我不能在类中有一个非整数静态const成员? - Why can't I have a non-integral static const member in a class? 带有waf的C ++头文件库 - C++ header-only library with waf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM