问题
C++11 introduces an object called std::ignore
:
const /* unspecified */ ignore;
For brevity, let
typedef decltype(std::ignore) T;
From what I can tell, the only requirement for T
is that it is CopyAssignable
, due to the specification of std::tie
[C++11, 20.4.2.4:7].
In g++-4.8, I find that T
is additionally DefaultConstructible
(e.g., T x;
compiles). Is this implementation-defined behavior?
(If there are other requirements on T
that I have missed, please elaborate.)
回答1:
The standard has no requirements on the type of ignore
, besides the fact that it is a type that is distinct from all other types.
Whatever machinery that a standard library container does to allow ignore
to gain the required behavior when used with tie
is up to that standard library implementation. The library may give it a template<T&> operator=(const T&)
overload, or it may use some other mechanism to make it work. The standard doesn't say. So it doesn't even have to be CopyAssignable
.
Note that tie
only has special behavior if you specifically use ignore
. If you use some other value, created by yourself (which, since the type has no requirements, you are not guaranteed to be able to do), you will get undefined behavior.
回答2:
From what I can tell, the only requirement for
T
is that it isCopyAssignable
, due to the specification ofstd::tie
[C++11, 20.4.2.4:7].
Formally, I don't think there is any requirement at all being placed. The fact that tie()
can accept ignore
as an argument does not mean that it has to store a value of that type in the tuple: although that's most likely what's going to happen in practice, I do not see this as being necessarily implied by the formal specification.
Is this implementation-defined behavior?
No, the behavior is unspecified, since the implementation is not required to document it (thanks to Pete Becker for clarifying this point).
来源:https://stackoverflow.com/questions/16721884/requirements-for-stdignore