有没有办法在compiletime打印一个constexpr字符串? [英] Is there a way to print a constexpr string during compiletime?

查看:114
本文介绍了有没有办法在compiletime打印一个constexpr字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做以下(仅下面的代码的相关部分):

I'm trying to do the following (only relevant parts of code below):

template<typename ContainerType>
struct IsContainerCheck : is_container<ContainerType>
{
   static constexpr char* err_value = "Type is not a container model";
};

namespace _check_concept {
    template<typename ResultType>
    struct run {
        constexpr static int apply() {
            static_assert(false, IsContainerCheck<ResultType>::err_value)
            return 0;
        }
    };

    template<>
    struct run<true_t> {
        constexpr static int apply() {
            return 0;
        }
    };
}

这会失败,因为static_assert只允许打印文字。同样是使用BOOST_STATIC_ASSERT_MSG宏。

This fails because the static_assert allows only literals to be printed. The same is with BOOST_STATIC_ASSERT_MSG macro.

所以我的问题是 - 是否有任何方法在编译期间输出一个constexpr字符串?

So my question is - is there any way to output a constexpr string during compilation? If there is a gcc extension providing this functionality that would also be great.

使用的编译器gcc 4.8.1

Used compiler gcc 4.8.1

推荐答案

GCC不提供这样的机制,你想要的。然而,如果你能够重构你的代码,如
后面的程序所示,你不需要
。 (我填充了几个缺口,以便给我们一个
可编译的例子):

GCC does not provide such a mechanism as you want. However you will not need it if you are able to refactor your code somewhat as illustrated in the following program. (I have filled in a few gaps so as to give us a compilable example):

#include <type_traits>
#include <vector>

template<typename ContainerType>
struct is_container
{
    static bool const value = false;
};

template<>
struct is_container<std::vector<int>>
{
    static bool const value = true;
};

template<typename ContainerType>
struct IsContainerCheck // : is_container<ContainerType> <- Uneccessary
{
    static_assert(is_container<ContainerType>::value, 
        "Type is not a container model");
};

namespace _check_concept {
    template<typename ResultType>
    struct run {
        constexpr static int apply() {
            return (IsContainerCheck<ResultType>(),0);
        }
    };

    // No such specialization is necessary. Delete it.
    // template<>
    // struct run<true_t> {
    //    constexpr static int apply() {
    //        return 0;
    //    }
    //};
}

using namespace _check_concept;

int main(int argc, char **argv)
{
    auto verdict0 = run<std::vector<int>>::apply();
    (void)verdict0;
    // The following line will static_assert: "Type is not a container model"
    auto verdict1 = run<float>::apply();
    (void)verdict1;
    return 0;
}

在您的专业化 _check_concept :: struct run< true_t> ; 我假设
true_t 不是别名或等效的 std :: true_type ,而是
只是某些 ResultType 的容器类型的占位符 。由于
IsContainerCheck< ResultType>()将会 static_assert , / code>运行运行 c> ,取决于 ResultType / code>。

In your specialization _check_concept::struct run<true_t> I presume that true_t is not an alias or equivalent of std::true_type, but rather just a place-holder for some ResultType that is a container type. As the test program shows, no such specialization is now necessary, because IsContainerCheck<ResultType>() will static_assert, or not, depending on ResultType, in the unspecialized run<ResultType>::apply().

这篇关于有没有办法在compiletime打印一个constexpr字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆