Constexpr if with a non-bool conditionIs a compiler forced to reject invalid constexpr?clang 3.3 and...

Does Skippy chunky peanut butter contain trans fat?

Does diversity provide anything that meritocracy does not?

Do "fields" always combine by addition?

After checking in online, how do I know whether I need to go show my passport at airport check-in?

Can I announce prefix 161.117.25.0/24 even though I don't have all of /24 IPs?

Why is Agricola named as such?

How do I append a character to the end of every line in an excel cell?

Why did Democrats in the Senate oppose the Born-Alive Abortion Survivors Protection Act (2019 S.130)?

Why are all my replica super soldiers young adults or old teenagers?

I have trouble understanding this fallacy: "If A, then B. Therefore if not-B, then not-A."

How does one write from a minority culture? A question on cultural references

A Missing Symbol for This Logo

Globe trotting Grandpa. Where is he going next?

How to make ice magic work from a scientific point of view?

How to visualize the Riemann-Roch theorem from complex analysis or geometric topology considerations?

Why would space fleets be aligned?

How to deal with possible delayed baggage?

How do I prevent a homebrew Grappling Hook feature from trivializing Tomb of Annihilation?

How to assess the long-term stability of a college as part of a job search

How can the probability of a fumble decrease linearly with more dice?

Hilchos Shabbos English Sefer

Is a new boolean field better than null reference when a value can be meaningfully absent?

How do you catch Smeargle in Pokemon Go?

What is a good reason for every spaceship to carry a weapon on board?



Constexpr if with a non-bool condition


Is a compiler forced to reject invalid constexpr?clang 3.3 and constexpr constraintsconstexpr bug in clang but not in gcc?Is function pointer comparison in a constexpr function allowed?When is std::string_view::operator== really constexpr?GCC and Clang disagree about C++17 constexpr lambda captures“if constexpr” interaction with “try in constexpr function” warningNested constexpr-if statement in discarded branch is still evaluated?std::variant modification in constexprC++ constexpr function in return statement













6















I seem to have found something that clang and gcc disagree on. Here's the code:



int main() {
if constexpr (2) {}
}


This successfully compiles with gcc 7.4.0 but fails with clang 7.0.0 with this error message:



test.cpp:3:17: error: constexpr if condition evaluates to 2, which cannot be narrowed to type 'bool'
[-Wc++11-narrowing]
if constexpr (2) {}
^
1 error generated.


cppreference doesn't seem to mention "narrowing" so this seems like a clang bug but I'm not entirely certain. If this is a bug with either compiler, has it already been reported?










share|improve this question























  • What if you do if constexpr (!!2) {} ?

    – Jesper Juhl
    1 hour ago











  • (!!2) will work, but 2 should work too.

    – ivan.ukr
    1 hour ago











  • I just checked. !!2 works with clang

    – Kerndog73
    1 hour ago
















6















I seem to have found something that clang and gcc disagree on. Here's the code:



int main() {
if constexpr (2) {}
}


This successfully compiles with gcc 7.4.0 but fails with clang 7.0.0 with this error message:



test.cpp:3:17: error: constexpr if condition evaluates to 2, which cannot be narrowed to type 'bool'
[-Wc++11-narrowing]
if constexpr (2) {}
^
1 error generated.


cppreference doesn't seem to mention "narrowing" so this seems like a clang bug but I'm not entirely certain. If this is a bug with either compiler, has it already been reported?










share|improve this question























  • What if you do if constexpr (!!2) {} ?

    – Jesper Juhl
    1 hour ago











  • (!!2) will work, but 2 should work too.

    – ivan.ukr
    1 hour ago











  • I just checked. !!2 works with clang

    – Kerndog73
    1 hour ago














6












6








6








I seem to have found something that clang and gcc disagree on. Here's the code:



int main() {
if constexpr (2) {}
}


This successfully compiles with gcc 7.4.0 but fails with clang 7.0.0 with this error message:



test.cpp:3:17: error: constexpr if condition evaluates to 2, which cannot be narrowed to type 'bool'
[-Wc++11-narrowing]
if constexpr (2) {}
^
1 error generated.


cppreference doesn't seem to mention "narrowing" so this seems like a clang bug but I'm not entirely certain. If this is a bug with either compiler, has it already been reported?










share|improve this question














I seem to have found something that clang and gcc disagree on. Here's the code:



int main() {
if constexpr (2) {}
}


This successfully compiles with gcc 7.4.0 but fails with clang 7.0.0 with this error message:



test.cpp:3:17: error: constexpr if condition evaluates to 2, which cannot be narrowed to type 'bool'
[-Wc++11-narrowing]
if constexpr (2) {}
^
1 error generated.


cppreference doesn't seem to mention "narrowing" so this seems like a clang bug but I'm not entirely certain. If this is a bug with either compiler, has it already been reported?







c++ language-lawyer c++17 implicit-conversion compiler-bug






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 1 hour ago









Kerndog73Kerndog73

572724




572724













  • What if you do if constexpr (!!2) {} ?

    – Jesper Juhl
    1 hour ago











  • (!!2) will work, but 2 should work too.

    – ivan.ukr
    1 hour ago











  • I just checked. !!2 works with clang

    – Kerndog73
    1 hour ago



















  • What if you do if constexpr (!!2) {} ?

    – Jesper Juhl
    1 hour ago











  • (!!2) will work, but 2 should work too.

    – ivan.ukr
    1 hour ago











  • I just checked. !!2 works with clang

    – Kerndog73
    1 hour ago

















What if you do if constexpr (!!2) {} ?

– Jesper Juhl
1 hour ago





What if you do if constexpr (!!2) {} ?

– Jesper Juhl
1 hour ago













(!!2) will work, but 2 should work too.

– ivan.ukr
1 hour ago





(!!2) will work, but 2 should work too.

– ivan.ukr
1 hour ago













I just checked. !!2 works with clang

– Kerndog73
1 hour ago





I just checked. !!2 works with clang

– Kerndog73
1 hour ago












2 Answers
2






active

oldest

votes


















5














Clang is diagnosing under these paragraphs




[stmt.if] (emphasis mine)



2 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
; this form is called a constexpr if statement.



[expr.const]



4 A converted constant expression of type T is an expression,
implicitly converted to type T, where the converted expression is a
constant expression and the implicit conversion sequence contains only




  • integral conversions other than narrowing conversions,




Now, when it comes to integral conversions, a conversion to bool is listed as an integral conversion. And it is narrowing, in the strictest sense of the word, since a bool cannot represent all the values of an int. So the diagnostic is not without grounds.



But I think it's also quite reasonable to consider the fact a conversion to bool is usually intended to check for "truthiness", and so the narrowing nature of it shouldn't matter. It looks like a minor bug in the standard, with GCC taking the common-sense route, and Clang adhering to the dry letter of the law in the strictest sense.






share|improve this answer


























  • A bug in the standard! LOL

    – Kerndog73
    52 mins ago











  • There is a proposal for this, P1401

    – Rakete1111
    43 secs ago



















3














We say it, but it's hidden. "contextually converted constant expression of type bool" is a standard term-of-art that excludes narrowing conversions.



Clang is correct.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54899466%2fconstexpr-if-with-a-non-bool-condition%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    Clang is diagnosing under these paragraphs




    [stmt.if] (emphasis mine)



    2 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
    ; this form is called a constexpr if statement.



    [expr.const]



    4 A converted constant expression of type T is an expression,
    implicitly converted to type T, where the converted expression is a
    constant expression and the implicit conversion sequence contains only




    • integral conversions other than narrowing conversions,




    Now, when it comes to integral conversions, a conversion to bool is listed as an integral conversion. And it is narrowing, in the strictest sense of the word, since a bool cannot represent all the values of an int. So the diagnostic is not without grounds.



    But I think it's also quite reasonable to consider the fact a conversion to bool is usually intended to check for "truthiness", and so the narrowing nature of it shouldn't matter. It looks like a minor bug in the standard, with GCC taking the common-sense route, and Clang adhering to the dry letter of the law in the strictest sense.






    share|improve this answer


























    • A bug in the standard! LOL

      – Kerndog73
      52 mins ago











    • There is a proposal for this, P1401

      – Rakete1111
      43 secs ago
















    5














    Clang is diagnosing under these paragraphs




    [stmt.if] (emphasis mine)



    2 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
    ; this form is called a constexpr if statement.



    [expr.const]



    4 A converted constant expression of type T is an expression,
    implicitly converted to type T, where the converted expression is a
    constant expression and the implicit conversion sequence contains only




    • integral conversions other than narrowing conversions,




    Now, when it comes to integral conversions, a conversion to bool is listed as an integral conversion. And it is narrowing, in the strictest sense of the word, since a bool cannot represent all the values of an int. So the diagnostic is not without grounds.



    But I think it's also quite reasonable to consider the fact a conversion to bool is usually intended to check for "truthiness", and so the narrowing nature of it shouldn't matter. It looks like a minor bug in the standard, with GCC taking the common-sense route, and Clang adhering to the dry letter of the law in the strictest sense.






    share|improve this answer


























    • A bug in the standard! LOL

      – Kerndog73
      52 mins ago











    • There is a proposal for this, P1401

      – Rakete1111
      43 secs ago














    5












    5








    5







    Clang is diagnosing under these paragraphs




    [stmt.if] (emphasis mine)



    2 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
    ; this form is called a constexpr if statement.



    [expr.const]



    4 A converted constant expression of type T is an expression,
    implicitly converted to type T, where the converted expression is a
    constant expression and the implicit conversion sequence contains only




    • integral conversions other than narrowing conversions,




    Now, when it comes to integral conversions, a conversion to bool is listed as an integral conversion. And it is narrowing, in the strictest sense of the word, since a bool cannot represent all the values of an int. So the diagnostic is not without grounds.



    But I think it's also quite reasonable to consider the fact a conversion to bool is usually intended to check for "truthiness", and so the narrowing nature of it shouldn't matter. It looks like a minor bug in the standard, with GCC taking the common-sense route, and Clang adhering to the dry letter of the law in the strictest sense.






    share|improve this answer















    Clang is diagnosing under these paragraphs




    [stmt.if] (emphasis mine)



    2 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
    ; this form is called a constexpr if statement.



    [expr.const]



    4 A converted constant expression of type T is an expression,
    implicitly converted to type T, where the converted expression is a
    constant expression and the implicit conversion sequence contains only




    • integral conversions other than narrowing conversions,




    Now, when it comes to integral conversions, a conversion to bool is listed as an integral conversion. And it is narrowing, in the strictest sense of the word, since a bool cannot represent all the values of an int. So the diagnostic is not without grounds.



    But I think it's also quite reasonable to consider the fact a conversion to bool is usually intended to check for "truthiness", and so the narrowing nature of it shouldn't matter. It looks like a minor bug in the standard, with GCC taking the common-sense route, and Clang adhering to the dry letter of the law in the strictest sense.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 44 mins ago

























    answered 54 mins ago









    StoryTellerStoryTeller

    99.9k12201271




    99.9k12201271













    • A bug in the standard! LOL

      – Kerndog73
      52 mins ago











    • There is a proposal for this, P1401

      – Rakete1111
      43 secs ago



















    • A bug in the standard! LOL

      – Kerndog73
      52 mins ago











    • There is a proposal for this, P1401

      – Rakete1111
      43 secs ago

















    A bug in the standard! LOL

    – Kerndog73
    52 mins ago





    A bug in the standard! LOL

    – Kerndog73
    52 mins ago













    There is a proposal for this, P1401

    – Rakete1111
    43 secs ago





    There is a proposal for this, P1401

    – Rakete1111
    43 secs ago













    3














    We say it, but it's hidden. "contextually converted constant expression of type bool" is a standard term-of-art that excludes narrowing conversions.



    Clang is correct.






    share|improve this answer




























      3














      We say it, but it's hidden. "contextually converted constant expression of type bool" is a standard term-of-art that excludes narrowing conversions.



      Clang is correct.






      share|improve this answer


























        3












        3








        3







        We say it, but it's hidden. "contextually converted constant expression of type bool" is a standard term-of-art that excludes narrowing conversions.



        Clang is correct.






        share|improve this answer













        We say it, but it's hidden. "contextually converted constant expression of type bool" is a standard term-of-art that excludes narrowing conversions.



        Clang is correct.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 54 mins ago









        T.C.T.C.

        107k14220326




        107k14220326






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54899466%2fconstexpr-if-with-a-non-bool-condition%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can't compile dgruyter and caption packagesLaTeX templates/packages for writing a patent specificationLatex...

            Schneeberg (Smreczany) Bibliografia | Menu...

            Hans Bellmer Spis treści Życiorys | Upamiętnienie | Przypisy | Bibliografia | Linki zewnętrzne |...