String literal with constexpr

I have a binary identifier which I tried to make a constexpr since all of its calculations would never occur during runtime (this is true for literal identifiers, right?). Since constant expressions can only have one instruction, I tried to cheat a little and returned an immediate call to a lambda function. This failed miserably however. I tried making a constexpr function pointer and called that from _binary down below, but the compiler still felt that it wasn't a constexpr. Why is this? And is there a way to make a function like _b below constexpr?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstddef>

using std::cout;
using std::endl;

long unsigned  //Original
    operator"" _b(const char* const literal, size_t lsize){
        long unsigned toreturn(0);
        for(size_t i = 0; i < lsize; ++i){
            if(*(literal+lsize-i-1) == '1')    toreturn += 1 << i;
        }
        return toreturn;
}
long unsigned  //Attempt with lambda function
    operator"" _bin(const char* const literal, size_t lsize){
        return [&](){
            long unsigned toreturn(0);
            for(size_t i = 0; i < lsize; ++i){
                if(*(literal+lsize-i-1) == '1')    toreturn += 1 << i;
            }
            return toreturn;
        }();
}
//Compiles fine
//Maybe I should be explicit and use 
//   "constexpr long unsigned (*ptr)(const char* const,size_t)" ?
constexpr auto ptr = [](const char* const literal, size_t lsize){
            long unsigned toreturn(0);
            for(size_t i = 0; i < lsize; ++i){
                if(*(literal+lsize-i-1) == '1')    toreturn += 1 << i;
            }
            return toreturn;
        };
//Does not compile
constexpr long unsigned  //Attempt with global function pointer
    operator"" _binary(const char* const literal, size_t lsize){
        return (*ptr)(literal,lsize);
}
int main(){
    cout << "111"_b << '\n'
        << "110011101"_b << '\n'
        << "00110111"_bin << '\n'
    << endl;
    return 0;
}
Function contained in lambda in non-constexpr (lambda itself is), so you cannot call it.

You should use recursion instead of iteration when dealing with constexpr functions.
Like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

constexpr
long unsigned parse(const char* const literal, size_t lsize, long unsigned current)
{
    return (lsize == 1)
              ?( (current << 1) + (literal[0] == '1' ? 1 : 0 ) )
              :parse(literal+1, lsize-1, (current << 1) + (literal[0] == '1' ? 1 : 0));
}

constexpr
long unsigned operator"" _binary(const char* const literal, size_t lsize)
{
    return parse(literal, lsize, 0);
}

int main()
{
    std::cout << "0101"_binary << std::endl;
    return 0;
}


EDIT: This code would look better if I have used templates (less ternary operators), but I am too lazy to rewrite it. Do it yourself if you want.
Last edited on
incidentally, C++14 will allow loops in constexpr functions, and will support binary literals directly. But until then, yes: recursion, ternaries, and throws are the language of constexpr programming.
I'm back and there is templated variant with normal literal:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

template <int, char...> struct literal;

template <int x> struct literal<x> {
  static const unsigned long
  to_int = x;
};

template <int x, char c, char ...cv> struct literal<x, c, cv...> {
  static const unsigned long
  to_int = literal<(x << 1) + (c == '1' ? 1 : 0), cv...>::to_int;
};

template <char ...cv>
constexpr unsigned long  operator "" _b()
{
  return literal<0, cv...>::to_int;
}

int main()
{
    std::cout << 1101_b << std::endl;
    return 0;
}
Alright, thanks for the suggestions. On a side note, how come you don't need to include the <cstddef> library for size_t?
Topic archived. No new replies allowed.