15

C++11 引入了一个名为 的对象std::ignore

const /* unspecified */ ignore;

为简洁起见,让

typedef decltype(std::ignore) T; 

据我所知,由于[C++11, 20.4.2.4:7]的规范,唯一的要求T是它。CopyAssignablestd::tie

在 g++-4.8 中,我发现这T是另外的DefaultConstructible(例如,T x;编译)。这是实现定义的行为吗?

(如果T我遗漏了其他要求,请详细说明。)

4

2 回答 2

15

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.

于 2013-05-23T19:11:48.427 回答
8

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].

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).

于 2013-05-23T19:12:00.173 回答