admin管理员组文章数量:1279237
I want to validate a c string at compile time (using static assert). I have already a constexpr function that is able to do. (I use C++17)
template <std::size_t N>
constexpr bool validate(const char (&name)[N]) {
/* ... */
return false;
}
Now I would like to add another constexpr function like this:
template <std::size_t N>
constexpr const char* VALIDATE(const char (&name)[N]) {
static_assert(validate(name), "wrong name");
return &name[0];
}
because I want to use it as follows:
class GenericClass {
public:
virtual const char* getName() const = 0;
};
class MyClass final: public GenericClass {
public:
const char* getName() const override {
return VALIDATE("MyClass");
}
};
I want to make sure at compile time that all classes derived from GenericClass have an appropriate name.
When I test this code I get the following compilation error:
"error: non-constant condition for static assertion" at static_assert(validate(name), "wrong name"); line
I have tried several things but I can't figure out how to call the validate function inside the VALIDATE function without losing the constexpr condition of the name given when VALIDATE is called with a constexpr C string.
Thanks
本文标签: c17check a c string at compile time in cStack Overflow
版权声明:本文标题:c++17 - check a c string at compile time in c++ - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741236221a2363045.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论