将全局constexpr类作为静态constexpr移动到类中

时间:2015-11-05 07:03:39

标签: c++ c++11 constexpr

我有一些全局while (Counter < 8) { console.log(Triangle); Triangle = Triangle + IncreaserOfTheNumberOfTriangles; Counter = Counter + 1; } constexpr我希望将Mask类作为Mask的一部分来减少main.cpp中的全局变量

目前正常运作

  • main.cpp

    static constexpr
  • Mask.hpp(已针对SO减少)

    constexpr Mask cg_completeMask(0xffffffffffffffffull, 0x1ffff);
    

我试图从main.cpp移动全局class Mask { unsigned long long m_64; unsigned int m_32; public: constexpr Mask(const unsigned long long ac_64, const unsigned int ac_32) : m_64(ac_64), m_32(ac_32) {} };

  • Mask.hpp

    Mask
  • Mask.cpp

    class Mask {
        unsigned long long m_64;
        unsigned int       m_32;
    
    public:        
        static constexpr Mask completeMask;
    
        constexpr Mask(const unsigned long long ac_64, const unsigned int ac_32) :
            m_64(ac_64),
            m_32(ac_32)
        {}
    };
    

我尝试过产生这些错误

constexpr Mask Mask::completeMask(0xffffffffffffffffull, 0x1ffff);

1 个答案:

答案 0 :(得分:1)

我能得到的最好是,这应该做你想要的 - 但不幸的是在界面中暴露你的常数。这是不可避免的,我很害怕。

* Mask.hpp

class Mask {
    unsigned long long m_64;
    unsigned int       m_32;


    // private constructor
    constexpr Mask(const unsigned long long ac_64, const unsigned int ac_32) :
    m_64(ac_64),
    m_32(ac_32)
    {}

public:

    // public declarations
    inline static constexpr Mask complete();
    inline static constexpr Mask partial();

};

// inline definitions
constexpr Mask Mask::complete()
{
    return Mask(0xffffffffffffffffull, 0x1ffff);
}

constexpr Mask Mask::partial()
{
    return Mask(0xf0f0f0f0f0f0f0f0ull, 0x1ffff);
}

使用示例:

auto main() -> int
{
    auto m1 = Mask::complete();
    auto m2 = Mask::partial();

    return 0;
}