我正在尝试执行以下操作(仅下面的相关代码部分):
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;
}
};
}
Run Code Online (Sandbox Code Playgroud)
这会失败,因为static_assert只允许打印文字.与BOOST_STATIC_ASSERT_MSG宏相同.
所以我的问题是 - 在编译期间有没有办法输出constexpr字符串?如果有一个gcc扩展提供这个功能也很棒.
使用的编译器gcc 4.8.1
海湾合作委员会没有提供你想要的机制.但是,如果您能够在某种程度上重构代码,则不需要它,如以下程序所示.(我填补了一些空白,以便给我们一个可编辑的例子):
#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;
}
Run Code Online (Sandbox Code Playgroud)
在您的专业_check_concept::struct run<true_t>我猜想
true_t是不是一个别名或等同std::true_type,而只是一个占位一些ResultType这是一个容器类型.作为测试程序显示,没有这样的专门现在有必要的,因为
IsContainerCheck<ResultType>()意志static_assert,还是不行,这取决于ResultType在非特run<ResultType>::apply().
| 归档时间: |
|
| 查看次数: |
838 次 |
| 最近记录: |