根据某些条件在编译时生成一个字符串

时间:2018-07-12 11:30:05

标签: c++ templates c++17 template-meta-programming constexpr

我想在编译时(fopen())生成file::get_mode()的开放模式参数

#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))

enum open_mode {
    read = (1u << 0),
    write = (1u << 1),
    binary = (1u << 2),
    update = (1u << 3)
};

template<int open_type>
class file {
public:
    constexpr static int is_read_mode() {
        return CHECK_BIT(open_type,0);
    }
    constexpr static int is_write_mode() {
        return CHECK_BIT(open_type,1);
    }
    constexpr static int is_binary_mode() {
        return CHECK_BIT(open_type,2);
    }
    constexpr static int is_update_mode() {
        return CHECK_BIT(open_type,3);
    }
    constexpr static const void get_mode(char (&mode)[5]) {
        int len = 0;
        if constexpr(is_read_mode())
            mode[len++] = 'r';
        if constexpr(is_write_mode())
            mode[len++] = 'w';
        if constexpr(is_binary_mode())
            mode[len++] = 'b';
        if constexpr(is_update_mode())
            mode[len++] = '+';
        mode[len++] = 0;
    }    
};

但是即使file::get_mode()中的所有内容都可以在编译时确定(据我所能判断),但我不认为它在编译时正在计算,因为以下代码由于以下原因未进行编译:最后的断言,删除最后的断言代码可以正常编译并运行

int main() {
    file<open_mode::write> f; 
    char mode[5];
    f.get_mode(mode);
    static_assert(mode[0] == 'w');
}

所以我的问题是:如何在编译时生成它?

2 个答案:

答案 0 :(得分:3)

您使用的是if constexpr,所以您使用的是C ++ 17。

在C ++ 17中,operator[]constexp,因此,您无需生成和修改C样式的数组,而可以生成返回std::array

我的意思是

constexpr static auto get_mode ()
    {
      std::array<char, 5U> ret {}; // initialize to `\0` all chars

      std::size_t pos = -1;

      if constexpr ( is_read_mode() ) ret[++pos] = 'r';
      if constexpr ( is_write_mode() ) ret[++pos] = 'w';
      if constexpr ( is_binary_mode() ) ret[++pos] = 'b';
      if constexpr ( is_update_mode() ) ret[++pos] = '+';

      return ret;
    }

所以你可以写成

static_assert( f.get_mode()[0] == 'w' );
static_assert( f.get_mode()[1] == '\0' );

通过:请记住使is_*_mode()函数返回bool,而不是int

以下是完整的编译示例

#include <array>

#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))

enum open_mode {
    read = (1u << 0),
    write = (1u << 1),
    binary = (1u << 2),
    update = (1u << 3)
};

template <int open_type>
struct file
 {
   constexpr static bool is_read_mode() { return CHECK_BIT(open_type,0); }
   constexpr static bool is_write_mode() { return CHECK_BIT(open_type,1); }
   constexpr static bool is_binary_mode() { return CHECK_BIT(open_type,2); }
   constexpr static bool is_update_mode() { return CHECK_BIT(open_type,3); }

   constexpr static auto get_mode ()
    {
      std::array<char, 5U> ret {};

      std::size_t pos = -1;

      if constexpr ( is_read_mode() ) ret[++pos] = 'r';
      if constexpr ( is_write_mode() ) ret[++pos] = 'w';
      if constexpr ( is_binary_mode() ) ret[++pos] = 'b';
      if constexpr ( is_update_mode() ) ret[++pos] = '+';

      return ret;
    }
 };

int main ()
 {
   file<open_mode::write> fw; 

   static_assert( fw.get_mode()[0] == 'w' );
   static_assert( fw.get_mode()[1] == '\0' );

   file<open_mode::read | open_mode::write> frw; 

   static_assert( frw.get_mode()[0] == 'r' );
   static_assert( frw.get_mode()[1] == 'w' );
   static_assert( frw.get_mode()[2] == '\0' );
 }

答案 1 :(得分:2)

char mode[5]不是constexpr,因此它可能会在运行时更改其值。相反,您可以从get_mode()返回字符串文字。您还应该修改逻辑以防止无效的访问模式(例如,如果要同时进行读取和写入,则应使用w+r+而不是wr)。

constexpr static const char* get_mode() {
    if (is_read_mode()) {
        if (is_binary_mode()) {
            return "rb";
        }
        else {
            return "r";
        }
    }
    // ...
}   

您可以在编译时像这样使用它:

int main() {
    file<open_mode::write> f; 
    static_assert(f.get_mode() == "w");
    static_assert(f.get_mode()[0] == 'w');
}
相关问题