Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

c++ - if constexpr(condition) as compile-time conditional

I want to use a constexpr bool (useF in the example below) to enable a feature in the following code. Here, calling A::f(). Additionally, I want to be the alias-template (a) to be void in the case I switch off the feature.

I tried to use a constexpr if statement, but the body is still being instantiated, which causes a compile error. If I use a wrapper template (X), the body is being discarded as I'd expected, but that seems ugly to me. Are there any other ways to do this?

constexpr bool useF = false;

struct A {
    static void f() {}
};

using a = std::conditional<useF, A, void>::type;

template<typename L>
struct X {
    static void h() {
        if constexpr(std::is_same<L, A>::value) {
            L::f(); // not instantiated, no error
        }
    }
};

int main() {
    if constexpr(useF) {
        a::f(); // error!?
    }

    X<a>::h();
}

I am using g++-7.0.1 with -std=c++17

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

if constexpr is only for templates. From [stmt.if]:

If the if statement is of the form if constexpr, the value of the condition shall be a contextually converted constant expression of type bool (5.20); this form is called a constexpr if statement. If the value of the converted condition is false, the first substatement is a discarded statement, otherwise the second substatement, if present, is a discarded statement. During the instantation of an enclosing templated entity (Clause 14), if the condition is not value-dependent after its instantiation, the discarded substatement (if any) is not instantiated.

Within X, the constexpr if statement will prevent the otherwise ill-formed statement from being instantiated. That is the goal of this language feature. But outside of templates, there's no such equivalent gain.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...