相关疑难解决方法(0)

嵌套结构尽管与全局结构完全相同但仍会破坏constexpr

我遇到以下代码时遇到问题:

template<typename T>
constexpr int get(T vec) {
  return vec.get();
}

struct coord {
  constexpr int get() const { return x; }
  int x;
};

struct foo {
    struct coord2 {
      constexpr int get() const { return x; }
      int x;
    };
    constexpr static coord f = { 5 };
    constexpr static int g = get(f); // works

    constexpr static coord2 h = { 5 };
    constexpr static int i = get(h); // doesn't work
};

constexpr coord foo::f;
constexpr …
Run Code Online (Sandbox Code Playgroud)

c++ constexpr c++11

18
推荐指数
1
解决办法
375
查看次数

C++中简单的constexpr LookUpTable 14

我试图基于一个整数数组创建一个简单的 LookUpTable,其思路是在编译时计算它.

试图使它可以用于我可能拥有的各种整数类型的任何其他未来表,我需要它作为模板.

所以我有一个LookUpTable.h

#ifndef LOOKUPTABLE_H
#define LOOKUPTABLE_H

#include <stdexcept> // out_of_range

template <typename T, std::size_t NUMBER_OF_ELEMENTS>
class LookUpTableIndexed
{
    private:
        //constexpr static std::size_t NUMBER_OF_ELEMENTS = N;

        // LookUpTable
        T m_lut[ NUMBER_OF_ELEMENTS ] {}; // ESSENTIAL T Default Constructor for COMPILE-TIME INTERPRETER!

    public:
        // Construct and Populate the LookUpTable such that;
        //   INDICES of values are MAPPED to the DATA values stored
        constexpr LookUpTableIndexed() : m_lut {}
        {
            //ctor
        }

        // Returns the …
Run Code Online (Sandbox Code Playgroud)

c++ lookup-tables constexpr c++14

8
推荐指数
1
解决办法
503
查看次数

constexpr结构成员初始化

此代码编译:

struct Info
{
    constexpr Info(bool val) : counted(false), value(unsigned(val)) {}
    constexpr Info(unsigned val) : counted(true), value(val) {}

    bool counted;
    unsigned value;
};

constexpr const auto data = std::array{
    Info{true}, Info{42u}
};

struct Foo
{
    constexpr static inline const auto data = std::array{
        Info{true}, Info{42u}
    };
};
Run Code Online (Sandbox Code Playgroud)

此代码不会:

struct Foo
{
    struct Info
    {
        constexpr Info(bool val) : counted(false), value(unsigned(val)) {}
        constexpr Info(unsigned val) : counted(true), value(val) {}

        bool counted;
        unsigned value;
    };

    constexpr static inline const auto data = …
Run Code Online (Sandbox Code Playgroud)

c++ constexpr c++17

5
推荐指数
1
解决办法
422
查看次数

标签 统计

c++ ×3

constexpr ×3

c++11 ×1

c++14 ×1

c++17 ×1

lookup-tables ×1