Created
October 30, 2020 01:08
-
-
Save omegacoleman/674aaa583938a800e152423e355d3e2d to your computer and use it in GitHub Desktop.
编译期重复给定字符串n遍
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <memory> | |
template <unsigned int Times> struct __attribute__((__packed__)) bracks | |
{ | |
char a = '{'; | |
char b = '}'; | |
bracks<Times - 1> next; | |
constexpr const char* get() const noexcept | |
{ | |
return reinterpret_cast<const char *>(std::addressof(a)); | |
} | |
}; | |
template <> struct __attribute__((__packed__)) bracks<0> { char csend = '\0'; }; | |
int main(void) | |
{ | |
bracks<8> br; | |
const char * s{br.get()}; | |
std::cout << std::string{s} << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment