auto Function with if Statement won't return a value

前端 未结 3 1851
醉话见心
醉话见心 2021-01-19 18:11

I made a template and an auto function that compare 2 values and return the smallest one. This is my code:

#include 

        
3条回答
  •  -上瘾入骨i
    2021-01-19 18:59

    As you have asked this question with the c++11 marker, I suppose you are using C++11. Unfortunately, the C++11 standard states that auto-type deduction (also for lambdas) is limited to a single statement.

    As the ?: operator is an expression instead of a statement, this will work while the if-else is a statement and doesn't meet the requirements.

    If you would compile this code with the C++14 standard, you will see that it should compile both cases as this limitation was removed.

提交回复
热议问题