anyone know why need “constexpr “?

2023-12-13 11:32:39

template<class Derived>
struct Base {
?? ?void name() {
?? ??? ?(static_cast<Derived*>(this))->impl();
?? ?}
};

struct D1 : public Base<D1> {
?? ?void impl() {
?? ??? ?std::cout << "D1::impl()\n";
?? ?}
};

struct D2 : public Base<D2> {
?? ?void impl() {
?? ??? ?std::cout << "D2::impl()\n";
?? ?}
};

struct D3 {
?? ?void impl() {
?? ??? ?std::cout << "D3::impl()\n";
?? ?}
};
?

template <class T>
void myFunction(T value) {
?? ?if constexpr (std::is_same_v<T, D1>) {
?? ??? ?// Code for float case
?? ??? ?std::cout << "D1 constexpr" << std::endl;
?? ??? ?value.name();

?? ?}
?? ?else {
?? ??? ?std::cout << "no D1 constexpr" << std::endl;
?? ??? ?// Code for general case
?? ?}
}
?

文章来源:https://blog.csdn.net/raidtest/article/details/134964351
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。