...
In this noncompliant code example, the expression a++
is not evaluated:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#include <iostream> void f() { int a = 14; int b = sizeof(a++); std::cout << a << ", " << b << std::endl; } |
...
In this compliant solution, the variable a
is incremented outside of the sizeof
operator:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#include <iostream> void f() { int a = 14; int b = sizeof(a); ++a; std::cout << a << ", " << b << std::endl; } |
...
In this noncompliant code example, the expression i++
is not evaluated within the decltype
specifier:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#include <iostream> void f() { int i = 0; decltype(i++) h = 12; std::cout << i; } |
...
In this compliant solution, i
is incremented outside of the decltype
specifier so that it is evaluated as desired:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#include <iostream> void f() { int i = 0; decltype(i) h = 12; ++i; std::cout << i; } |
...
The following code is an example of compliant code using an unevaluated operand in a macro definition:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
void small(int x); void large(long long x); #define m(x) \ do { if (sizeof(x) == sizeof(int)) { \ small(x); \ } else if (sizeof(x) == sizeof(long long)) { \ large(x); \ } \ } while (0) void f() { int i = 0; m(++i); } |
...
The following code is an example of compliant code using an unevaluated operand in a SFINAE context to determine whether a type can be postfix incremented:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#include <iostream> #include <type_traits> #include <utility> template <typename T> class is_incrementable { typedef char one[1]; typedef char two[2]; static one &is_incrementable_helper(decltype(std::declval<typename std::remove_cv<T>::type&>()++) *p); static two &is_incrementable_helper(...); public: static const bool value = sizeof(is_incrementable_helper(nullptr)) == sizeof(one); }; void f() { std::cout << std::boolalpha << is_incrementable<int>::value; } |
In an instantiation of is_incrementable
, the use of the postfix increment operator generates side effects that are used to determine whether the type is postfix incrementable. However, the value result of these side effects is discarded, so the side effects are used only for SFINAE.
...