unknown.prince
unknown.prince

Reputation: 752

generate a string at compile time based on some condition

I wanted to generate fopen()'s open mode parameter at compile-time (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;
    }    
};

but even though everything inside the file::get_mode() can be determined at compile-time (as far as I can think) but i don't think it's computing at compile-time as the following code is not compiling because of the last assertion, removing the last assertion code compiles fine and runs

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

so my question is: how can I generate this at compile-time?

Upvotes: 2

Views: 168

Answers (2)

max66
max66

Reputation: 66240

You're using if constexpr so you're using C++17.

In C++17 the operator[] is constexp so, instead of passing and modifying a C-style array, you can generate an return a std::array.

I mean

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;
    }

so you can write something as

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

En passant: remember to make is_*_mode() functions returning bool, not int.

The following is a full compiling example

#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' );
 }

Upvotes: 3

VLL
VLL

Reputation: 10165

char mode[5] is not constexpr, so it might change its values in runtime. Instead, you could return a string literal from get_mode(). You should also modify the logic to prevent invalid access modes (for example, if you want both read and write, you should use w+ or r+, not wr).

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

You can use it at compile time like this:

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

Upvotes: 2

Related Questions