看到了怪东西
实现效果
先从知乎拿点黑魔法
stmp_counter.hpp
#ifndef _STMP_COUNTER
#define _STMP_COUNTER
#include <cstddef>
#include <cstdint>
template<std::size_t N>
struct reader
{
friend auto counted_flag(reader<N>);
};
template<std::size_t N>
struct setter
{
friend auto counted_flag(reader<N>) {}
std::size_t value = N;
};
template<auto N = 0, auto tag = []{}, bool condition = requires(reader<N> red){ counted_flag(red); }>
constexpr auto next_censored_counter()
{
if constexpr (!condition)
{
constexpr setter<N> s;
return s.value;
}
else
{
return next_censored_counter<N + 1>();
}
}
template<auto N = 0, auto tag = []{}, bool condition = requires(reader<N> red){ counted_flag(red); }>
constexpr auto this_censored_counter()
{
if constexpr (!condition)
{
return N - 1;
}
else
{
return this_censored_counter<N + 1>();
}
}
#endif
然后整个字符串类
censored_strings.hpp
#ifndef _CENSORED_STRING_DEFS_HPP
#define _CENSORED_STRING_DEFS_HPP
#include <string>
#include <type_traits>
#include <utility>
#include "stmp_counter.hpp"
template <std::size_t N> struct MetaString
{
char p[N]{};
std::size_t size = N;
constexpr MetaString(char const (&pp)[N]) { std::ranges::copy(pp, p); };
};
template <size_t N> struct censored_strings
{
};
template <> struct censored_strings<next_censored_counter()>
{
template <std::size_t N> static consteval bool check(MetaString<N> str)
{
return false;
}
};
#define 备案字符串(s) \
template <> struct censored_strings<next_censored_counter()> \
{ \
inline static constexpr auto str = MetaString(s); \
\
template <std::size_t N> \
static consteval bool check(MetaString<N> str1) \
{ \
return std::char_traits<char>::compare(str.p, str1.p, N) == 0 || \
censored_strings<this_censored_counter() - 1>::check(str1); \
} \
};
#include "censore_strings_defs.hpp"
using last_censored_string = censored_strings<next_censored_counter() - 1>;
#endif
就能这样注册字符串
censore_strings_defs.hpp
#ifndef 备案字符串
#define 备案字符串(...)
#endif
备案字符串("已备案字符串");
备案字符串("Hello world");
printf
部分一个简单的检测就行
censored-printf.hpp
#include <cassert>
#include "censored_strings.hpp"
template <std::size_t N> struct CensoredString
{
char p[N]{};
std::size_t size = N;
private:
constexpr CensoredString(char const (&pp)[N]) { std::ranges::copy(pp, p); };
template <MetaString A> friend constexpr auto operator""_csv();
};
template <MetaString A> constexpr auto operator""_mv() { return A; }
template <MetaString A> constexpr auto operator""_csv()
{
static_assert(last_censored_string::check(A), "不得输出未备案字符串");
return CensoredString(A.p);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
template <size_t N, typename... Args>
inline int censored_printf_(CensoredString<N> meta_fmt, Args &&...args)
{
return printf(meta_fmt.p, std::forward<Args>(args)...);
}
#pragma GCC diagnostic pop
#define censored_printf(fmt, ...) censored_printf_(fmt ""_csv, ##__VA_ARGS__)
最后
main.cpp
#include <iostream>
#include <string>
#include <string_view>
#include <typeinfo>
#include "censored-printf.hpp"
using namespace std::string_view_literals;
int main(void)
{
censored_printf("已备案字符串");
censored_printf("未备案字符串");
return 0;
}