| 1 |
/* { dg-do compile } */ |
| 2 |
/* { dg-options "-std=c++14 -O -fdump-tree-dse1-details" } */ |
| 3 |
|
| 4 |
using uint = unsigned int; |
| 5 |
|
| 6 |
template<typename C, uint S> |
| 7 |
struct FixBuf |
| 8 |
{ |
| 9 |
C buf[S] = {}; |
| 10 |
}; |
| 11 |
|
| 12 |
template<typename C> |
| 13 |
struct OutBuf |
| 14 |
{ |
| 15 |
C* cur; |
| 16 |
C* end; |
| 17 |
C* beg; |
| 18 |
|
| 19 |
template<uint S> |
| 20 |
constexpr |
| 21 |
OutBuf(FixBuf<C, S>& b) : cur{b.buf}, end{b.buf + S}, beg{b.buf} { } |
| 22 |
|
| 23 |
OutBuf(C* b, C* e) : cur{b}, end{e} { } |
| 24 |
OutBuf(C* b, uint s) : cur{b}, end{b + s} { } |
| 25 |
|
| 26 |
constexpr |
| 27 |
OutBuf& operator<<(C v) |
| 28 |
{ |
| 29 |
if (cur < end) { |
| 30 |
*cur = v; |
| 31 |
} |
| 32 |
++cur; |
| 33 |
return *this; |
| 34 |
} |
| 35 |
|
| 36 |
constexpr |
| 37 |
OutBuf& operator<<(uint v) |
| 38 |
{ |
| 39 |
uint q = v / 10U; |
| 40 |
uint r = v % 10U; |
| 41 |
if (q) { |
| 42 |
*this << q; |
| 43 |
} |
| 44 |
*this << static_cast<C>(r + '0'); |
| 45 |
return *this; |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
template<bool BOS> |
| 50 |
struct BufOrSize |
| 51 |
{ |
| 52 |
template<typename C, uint S> |
| 53 |
static constexpr auto Select(FixBuf<C, S>& fb, OutBuf<C>&) |
| 54 |
{ |
| 55 |
return fb; |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
template<> |
| 60 |
struct BufOrSize<true> |
| 61 |
{ |
| 62 |
template<typename C, uint S> |
| 63 |
static constexpr auto Select(FixBuf<C, S>&, OutBuf<C>& ob) |
| 64 |
{ |
| 65 |
return ob.cur - ob.beg; |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
// if BOS=1, it will return the size of the generated data, else the data itself |
| 70 |
template<uint N, uint S, bool BOS = 0> |
| 71 |
constexpr |
| 72 |
auto fixbuf() |
| 73 |
{ |
| 74 |
FixBuf<char, S> fb; |
| 75 |
OutBuf<char> ob{fb}; |
| 76 |
for (uint i = 0; i <= N; ++i) { |
| 77 |
ob << i << static_cast<char>(i == N ? 0 : ' '); |
| 78 |
} |
| 79 |
return BufOrSize<BOS>::Select(fb, ob); |
| 80 |
} |
| 81 |
|
| 82 |
auto foo() |
| 83 |
{ |
| 84 |
constexpr auto x = fixbuf<13, 200>(); |
| 85 |
return x; |
| 86 |
} |
| 87 |
|
| 88 |
auto foo_sized() |
| 89 |
{ |
| 90 |
constexpr auto s = fixbuf<13, 0, 1>(); |
| 91 |
constexpr auto x = fixbuf<13, s>(); |
| 92 |
return x; |
| 93 |
} |
| 94 |
|
| 95 |
int main() |
| 96 |
{ |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/* { dg-final { scan-tree-dump-times "MEM <char\\\[\[0-9\]+]> \\\[\\(struct FixBuf \\*\\)&<retval> \\+ \[0-9\]+B\\\] = {}" 1 "dse1" } } */ |