c++ - 打印类型包括装饰、模板元编程、constexpr,用什么?

标签 c++ metaprogramming c++14 constexpr typeinfo

我有一个专门用于函数的 Trait:

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 T>
struct GetTypeInfoString {};

它专用于所有未装饰的类型,但我也想打印装饰的类型。我是这样使用它的:

extern constexpr auto intstr = makeStringLiteral("int");

template <>
struct GetTypeInfoString<int> {
   static constexprt auto & value = intstr;
};

现在我已经有了基本信息,我想实现一个constexpr函数:

template <class T>
constexpr const char * getTypeInfo() {
    //Something here...
}

我的目标是打印带有装饰的字体,而不仅仅是基本字体。即:int const * [][3]等...

最佳答案

问题基本上是如何得到这个:

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;
}

为了产生这个:

const int
const int&
int&&
const volatile int&

以一种constexpr的方式。

回答:

#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;
}

产生输出:

const int
const int&
int&&
const volatile int&

更新:

一个更完整/可靠的例子:

#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;
}

预期输出:

int const
int const&
int&&
int const volatile&
int const volatile* const* volatile* const volatile** const*&

关于c++ - 打印类型包括装饰、模板元编程、constexpr,用什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39247031/

相关文章:

python - 在运行时向 python 添加模块

c++ - 从数组的类型中查找元素的类型

python - 类声明内部调用的函数如何?

c++ - 如何有条件地在具有相同签名的两个构造函数之间切换?

c++ - 读取键盘字符以确定快捷方式

c++ - future::wait() 是否与 async() 执行线程的完成同步?

c++ - 抽象类型可以用作纯虚函数的返回类型吗?

c++ - TColor Initialise with int 适用于 {} 但不适用于 ()

c++ - 传递静态 constexpr 成员而没有按值定义的奇怪行为

C++:将模板函数作为参数传递给其他模板函数时的模板参数推导