简体   繁体   中英

Pre-C++14 template metaprogramming and conditional operator

From cppreference.com :

Such conditional operator was commonly used in C++11 constexpr programming prior to C++14.

 std::string str = 2+2==4 ? "ok" : throw std::logic_error("2+2 != 4"); 

What does cppreference refer to? What was the pre-C++14 idiom and why in C++14 that technique is no longer relevant?

In you could not have more than one statement, basically, in a constexpr function. In you can.

constexpr bool str(int x){
  return  2+2==x ? true : throw std::logic_error("2+2 != x");
}

vs :

constexpr bool str(int x){
  if (2+2==x)
    return true;
  else
     throw std::logic_error("2+2 != x");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM