C++ 打印类型包括装饰、模板元编程、constexpr、使用什么?
我有这样一个特性,专门用于函数:C++ 打印类型包括装饰、模板元编程、constexpr、使用什么?,c++,metaprogramming,c++14,constexpr,typeinfo,C++,Metaprogramming,C++14,Constexpr,Typeinfo,我有这样一个特性,专门用于函数: template <class Ret, class...Args> struct FunctionTraits<Ret (*)(Args...)> { using ReturnType = Ret; template <std::size_t> using Parameter = std::tuple_element_t<std::tuple<Args...>>; }; 模板 结
template <class Ret, class...Args>
struct FunctionTraits<Ret (*)(Args...)> {
using ReturnType = Ret;
template <std::size_t>
using Parameter = std::tuple_element_t<std::tuple<Args...>>;
};
模板
结构功能特性{
使用ReturnType=Ret;
模板
使用参数=std::tuple\u element\u t;
};
现在我想打印函数的签名,而不丢失装饰符
为此,我实现了如下元函数:
template <class T>
struct GetTypeInfoString {};
extern constexpr auto intstr = makeStringLiteral("int");
template <>
struct GetTypeInfoString<int> {
static constexprt auto & value = intstr;
};
模板
结构GetTypeInfoString{};
它是专门为每一个未装饰的类型,但我也想打印装饰类型。我是这样使用它的:
template <class T>
struct GetTypeInfoString {};
extern constexpr auto intstr = makeStringLiteral("int");
template <>
struct GetTypeInfoString<int> {
static constexprt auto & value = intstr;
};
extern constexpr auto intstr=makeStringLiteral(“int”);
模板
结构GetTypeInfoString{
静态constexprt auto&value=intstr;
};
现在我已经有了基本信息,我想实现一个constexpr函数:
template <class T>
constexpr const char * getTypeInfo() {
//Something here...
}
模板
constexpr const char*getTypeInfo(){
//这里有些东西。。。
}
我的目标是打印带有装饰的字体,而不仅仅是基本的字体。也就是说:
int const*[][3]
,等等。调整:花了几个小时,但无论如何
#include <iostream>
#include <string>
#include <sstream>
#include <tuple>
#include <vector>
template <typename... T> struct type_info;
template <typename... T> struct tail_type_info;
// prints the tail of a <typename...T> type argpack
// Needed to insert commas in the appropriate places
template <typename H, typename... T>
struct tail_type_info<H,T...>
{
std::ostream & operator()(std::ostream& o) {
type_info<H>p;
o << ", ";
p(o);
tail_type_info<T...> x;
return x(o);
}
};
// if the tail is empty, do nothing
template <>
struct tail_type_info<>
{
std::ostream & operator()(std::ostream& o) {
return o;
}
};
// specialization for the base types
template <> struct type_info<void> {
std::ostream& operator()(std::ostream& o) {
o << "void";
return o;
}
};
template <> struct type_info<bool> {
std::ostream& operator()(std::ostream& o) {
o << "bool";
return o;
}
};
template <> struct type_info<int8_t> {
std::ostream& operator()(std::ostream& o) {
o << "int8_t";
return o;
}
};
template <> struct type_info<uint8_t> {
std::ostream& operator()(std::ostream& o) {
o << "uint8_t";
return o;
}
};
template <> struct type_info<int32_t> {
std::ostream& operator()(std::ostream& o) {
o << "int32_t";
return o;
}
};
template <> struct type_info<uint32_t> {
std::ostream& operator()(std::ostream& o) {
o << "uint32_t";
return o;
}
};
template <> struct type_info<int64_t> {
std::ostream& operator()(std::ostream& o) {
o << "int64_t";
return o;
}
};
template <> struct type_info<uint64_t> {
std::ostream& operator()(std::ostream& o) {
o << "uint64_t";
return o;
}
};
// TODO fill it in for the rest of the types (char, short, wchar_t)
// compound types (&, *, CV, [N], [])
template <typename T>
struct type_info<T*> {
std::ostream& operator()(std::ostream& o) {
type_info<T> p;
p(o) << " *";
return o;
}
};
template <typename T>
struct type_info<T&> {
std::ostream& operator()(std::ostream& o) {
type_info<T> p;
p(o) << " &";
return o;
}
};
template <typename T>
struct type_info<const T> {
std::ostream& operator()(std::ostream& o) {
type_info<T>p;
p(o) << " const";
return o;
}
};
template <typename T>
struct type_info<volatile T> {
std::ostream& operator()(std::ostream& o) {
type_info<T>p;
p(o) << " volatile";
return o;
}
};
template <typename T, size_t N>
struct type_info<T[N]> {
std::ostream& operator()(std::ostream& o) {
type_info<T>p;
p(o) << "[" << N << "]";
return o;
}
};
template <typename T>
struct type_info<T[]> {
std::ostream& operator()(std::ostream& o) {
type_info<T>p;
p(o) << "[]";
return o;
}
};
// specialization for std::containers
template <typename T>
struct type_info<std::vector<T>> {
std::ostream& operator()(std::ostream& o) {
type_info<T>p;
o << "std::vector<";
p(o);
return o << ">";
}
};
// TODO define specializations for other containers as necessary
// specialization for std::tuple as an example of variadic template types
template <typename H, typename... T>
struct type_info<std::tuple<H, T...>> {
std::ostream& operator()(std::ostream& o) {
type_info<H>p;
tail_type_info<T...>tailp;
o << "std::tuple<";
p(o);
tailp(o);
return o << ">";
}
};
template <typename H>
struct type_info<std::tuple<H>> {
std::ostream& operator()(std::ostream& o) {
type_info<H>p;
o << "std::tuple<";
p(o);
return o << ">";
}
};
template <>
struct type_info<std::tuple<>> {
std::ostream& operator()(std::ostream& o) {
o << "std::tuple<";
return o << ">";
}
};
// specialization for functions
// function with no args
template <typename R>
struct type_info<R(*)(void)>
{
std::ostream& operator()(std::ostream& o) {
type_info<R>p;
p(o) << " (*)(void)";
return o;
}
};
// function with 1 arg
template <typename R, typename A0>
struct type_info<R(*)(A0)>
{
std::ostream& operator()(std::ostream& o) {
type_info<R>p;
type_info<A0>a;
p(o) << " (*)(";
a(o) << ")";
return o;
}
};
// function with multiple args
template <typename R, typename Ah, typename... At>
struct type_info<R(*)(Ah, At...)>
{
std::ostream& operator()(std::ostream& o) {
type_info<R>p;
type_info<Ah>a;
tail_type_info<At...>a_tail;
p(o) << " (*)(";
a(o);
a_tail(o);
o << ")";
return o;
}
};
template<typename... T> std::ostream& operator << (std::ostream& o, type_info<T...>& v) {
return v(o);
}
template <typename R, typename... A>
std::string FunctionSignatureStr(R(A...)&) {
std::stringstream dest;
type_info<R(*)(A...)> funcTrace;
funcTrace(dest);
return dest.str();
}
std::vector<long> dummyFunc(std::tuple<const long*[4], volatile bool& > const&) {
return std::vector<long>();
}
int main() {
std::cout << FunctionSignatureStr(dummyFunc) << std::endl;
type_info<std::vector<long>(*)(std::tuple<const long*, volatile bool&> const&)> d;
std::cout << d << std::endl;
}
#包括
#包括
#包括
#包括
#包括
模板结构类型信息;
模板结构尾部类型信息;
//打印类型argpack的尾部
//需要在适当的位置插入逗号
模板
结构尾部类型信息
{
std::ostream&operator()(std::ostream&o){
键入infop;
O.P>这没有标准的C++库函数,但是大多数编译器确实提供了一些用于签名的方法。
对于gcc,看起来是这样的
#include <cxxabi.h>
#include <iostream>
template <class T>
constexpr std::string getTypeInfo() {
int status=0;
char *t=abi::__cxa_demangle(typeid(T).name(), 0, 0, &status);
std::string s=t;
free(t);
return s;
}
template<class C, class T>
void foobar(C c, T t)
{
}
class Z {};
int main()
{
std::cout << getTypeInfo<int>() << std::endl;
std::cout << getTypeInfo<Z *[5]>() << std::endl;
auto *p= &foobar<int, char>;
std::cout << getTypeInfo<decltype(p)>() << std::endl;
}
问题基本上是如何做到这一点:
int main()
{
std::cout << TypeInfo<const int>::value() << std::endl;
std::cout << TypeInfo<const int&>::value() << std::endl;
std::cout << TypeInfo<int&&>::value() << std::endl;
std::cout << TypeInfo<const volatile int&>::value() << std::endl;
}
以一种坚定的方式
答复:
#include <iostream>
#include <tuple>
template <class T>
struct TypeInfo;
template<std::size_t N>
struct immutable_string
{
constexpr immutable_string(const char (&s)[N])
: _data {}
{
for (std::size_t i = 0 ; i < N ; ++i)
_data[i] = s[i];
}
constexpr immutable_string()
: _data {}
{
}
constexpr char& operator[](std::size_t i) { return _data[i]; }
constexpr const char& operator[](std::size_t i) const { return _data[i]; }
using ref = const char (&)[N];
constexpr ref data() const { return _data; }
static constexpr std::size_t size() { return N-1; }
char _data[N];
};
template<std::size_t N>
std::ostream& operator<<(std::ostream& os, immutable_string<N> s)
{
return os.write(s.data(), s.size());
}
template<std::size_t LN, std::size_t RN>
constexpr auto operator+(immutable_string<LN> l, immutable_string<RN> r)
{
constexpr std::size_t len = LN + RN - 2;
immutable_string<len + 1> result;
std::size_t i = 0;
for ( ; i < (LN-1) ; ++i)
{
result[i] = l[i];
}
for (auto j = 0 ; j < (RN-1) ; ++j)
{
result[i + j] = r[j];
}
return result;
}
template<std::size_t N>
constexpr auto literal(const char (&s)[N])
{
return immutable_string<N>(s);
}
template <>
struct TypeInfo<int> {
static constexpr auto value() { return literal("int"); }
};
template<class T>
struct TypeInfo<const T>
{
static constexpr auto value() { return literal("const ") + TypeInfo<T>::value(); }
};
template<class T>
struct TypeInfo<volatile T>
{
static constexpr auto value() { return literal("volatile ") + TypeInfo<T>::value(); }
};
template<class T>
struct TypeInfo<const volatile T>
{
static constexpr auto value() { return literal("const volatile ") + TypeInfo<T>::value(); }
};
template<class T>
struct TypeInfo<T&>
{
static constexpr auto value() { return TypeInfo<T>::value() + literal("&"); }
};
template<class T>
struct TypeInfo<T&&>
{
static constexpr auto value() { return TypeInfo<T>::value() + literal("&&"); }
};
int main()
{
std::cout << TypeInfo<const int>::value() << std::endl;
std::cout << TypeInfo<const int&>::value() << std::endl;
std::cout << TypeInfo<int&&>::value() << std::endl;
std::cout << TypeInfo<const volatile int&>::value() << std::endl;
}
更新:
一个更完整/可靠的示例:
#include <iostream>
template <class T>
struct TypeInfo;
template<std::size_t N>
struct immutable_string
{
constexpr immutable_string(const char (&s)[N])
: _data {}
{
for (std::size_t i = 0 ; i < N ; ++i)
_data[i] = s[i];
}
constexpr immutable_string()
: _data {}
{
}
constexpr char& operator[](std::size_t i) { return _data[i]; }
constexpr const char& operator[](std::size_t i) const { return _data[i]; }
using ref = const char (&)[N];
constexpr ref data() const { return _data; }
static constexpr std::size_t size() { return N-1; }
char _data[N];
};
template<std::size_t N>
std::ostream& operator<<(std::ostream& os, immutable_string<N> s)
{
return os.write(s.data(), s.size());
}
template<std::size_t LN, std::size_t RN>
constexpr auto operator+(immutable_string<LN> l, immutable_string<RN> r)
{
constexpr std::size_t len = LN + RN - 2;
immutable_string<len + 1> result;
std::size_t i = 0;
for ( ; i < (LN-1) ; ++i)
{
result[i] = l[i];
}
for (auto j = 0 ; j < (RN-1) ; ++j)
{
result[i + j] = r[j];
}
return result;
}
template<std::size_t N>
constexpr auto literal(const char (&s)[N])
{
return immutable_string<N>(s);
}
template <>
struct TypeInfo<int> {
static constexpr auto value() { return literal("int"); }
};
template<class T>
struct TypeInfo<const T>
{
static constexpr auto value() { return TypeInfo<T>::value() + literal(" const"); }
};
template<class T>
struct TypeInfo<volatile T>
{
static constexpr auto value() { return TypeInfo<T>::value() + literal(" volatile"); }
};
template<class T>
struct TypeInfo<const volatile T>
{
static constexpr auto value() { return TypeInfo<T>::value() + literal(" const volatile"); }
};
template<class T>
struct TypeInfo<T&>
{
static constexpr auto value() { return TypeInfo<T>::value() + literal("&"); }
};
template<class T>
struct TypeInfo<T&&>
{
static constexpr auto value() { return TypeInfo<T>::value() + literal("&&"); }
};
template<class T>
struct TypeInfo<T*>
{
static constexpr auto value() { return TypeInfo<T>::value() + literal("*"); }
};
int main()
{
std::cout << TypeInfo<const int>::value() << std::endl;
std::cout << TypeInfo<const int&>::value() << std::endl;
std::cout << TypeInfo<int&&>::value() << std::endl;
std::cout << TypeInfo<const volatile int&>::value() << std::endl;
std::cout << TypeInfo<const volatile int* const* volatile * const volatile **const *&>::value() << std::endl;
}
使用文本字符串的gnu扩展,您可以
template <typename Char, Char...Cs>
struct LString
{
static constexpr Char value[] = {Cs..., 0};
};
template <typename Char, Char...Cs> constexpr Char LString<Char, Cs...>::value[];
// string literal operator templates are a GNU extension
template <typename Char, Char...Cs>
constexpr LString<Char, Cs...> operator ""_ls()
{
return {};
}
template <typename Char, Char...Lhs, Char...Rhs>
constexpr LString<Char, Lhs..., Rhs...>
operator + (const LString<Char, Lhs...>&, const LString<Char, Rhs...>&)
{
return {};
}
template <typename Stream, typename Char, Char...Cs>
Stream& operator << (Stream& s, const LString<Char, Cs...>&ls)
{
return s << ls.value;
}
模板
结构LString
{
静态constexpr字符值[]={Cs…,0};
};
模板constepr Char LString::value[];
//字符串文字运算符模板是GNU扩展
模板
constexpr LString运算符“”\u ls()
{
返回{};
}
模板
constexpr-LString
运算符+(常量LString&,常量LString&)
{
返回{};
}
模板
Stream&operator您是用它来向用户打印信息还是诊断模板问题?我有一个表来描述API,并生成所有元数据。我想打印完整签名和其他数据。我正在实现一个小的可执行文件,可以查询API的参数和其他内容。毫无疑问,CV限定符将是带有abi“getTypeInfo()”的st返回“int volatile*”。我需要一个可移植的解决方案。然后答案是:没有可移植的解决方案。@SamVarshavchik是的,有。但不是一个简单的解决方案,考虑到需要使用argpack模板获取函数的类型。好的,这里的关键点是我卡住了…static constexpr auto value variable…使它成为一个函数,它就可以工作了!!太棒了dea.非常感谢。我会接受这个答复。不可变字符串
akastd::string\u视图
甚至std::array
@black constexpr std::array在c++17之前是不可变的。字符串视图不能进行编译时连接,所以不幸的是两者都不合适。遗憾的是它没有进入标准。这将是真的非常有用。
int const
int const&
int&&
int const volatile&
int const volatile* const* volatile* const volatile** const*&
template <typename Char, Char...Cs>
struct LString
{
static constexpr Char value[] = {Cs..., 0};
};
template <typename Char, Char...Cs> constexpr Char LString<Char, Cs...>::value[];
// string literal operator templates are a GNU extension
template <typename Char, Char...Cs>
constexpr LString<Char, Cs...> operator ""_ls()
{
return {};
}
template <typename Char, Char...Lhs, Char...Rhs>
constexpr LString<Char, Lhs..., Rhs...>
operator + (const LString<Char, Lhs...>&, const LString<Char, Rhs...>&)
{
return {};
}
template <typename Stream, typename Char, Char...Cs>
Stream& operator << (Stream& s, const LString<Char, Cs...>&ls)
{
return s << ls.value;
}
template <typename T> struct TypeInfo;
template <> struct TypeInfo<int> { static constexpr auto value = "int"_ls; };
template<class T> struct TypeInfo<const T>
{
static constexpr auto value = "const "_ls + TypeInfo<T>::value;
};
// ...