11

Sometimes it is necessary to pass a dummy value without any data to some template. For example:

template <typename X, typename Y> struct BoundaryConditions { X x; Y y; BoundaryConditions(typename X::init xi, typename Y::init yi) : x(xi), y(yi) { ... } }; 

We may wish to implement free boundary conditions that doesn't take any parameters. It's pretty easy to implement such a thing with type checking:

struct Nothing {}; Nothing nothing = Nothing(); struct Free { typedef Nothing init; ... }; BoundaryConditions<Free, Fixed> foo(nothing, 100); 

So the matter of my question: is there an implementation of something like my Nothing type in the standard library or boost?

0

5 Answers 5

14

You can use empty tuple. Like std::tuple<>();

Sign up to request clarification or add additional context in comments.

1 Comment

@ziggystar, you comment under the question starts with You are looking for a unit type, and the comment here with The empty tuple is the unit type; by transitivity this implies You are looking for the empty tuple, which is what this answer gives. Why shouldn't it be accepted?
5

How about boost::none and boost::none_t?

http://www.boost.org/doc/libs/1_54_0/boost/none.hpp

1 Comment

Probably the best match but the tuple solution requires only std.
4

The usual solution is to use void, but this requires partial specialization of the template (which is also usual, since otherwise, it requires more space).

1 Comment

Also besides requiring more space this does not work at all if there is a function returning T& since void & can't be returned. I think it does not really matter which type is used, an empty struct None {}; does the trick already as it will be zero size.
2

void seems like a natural way, since it is > void <. However C++ won't allow instantiating this type hence it is useless when having to declaring numerous template classes. The typical use of a separate type that allows that but denotes an 'empty type' would be a template event bus specialized for 'no data' provided in each event. Such as:

#include <variant> typedef std::monostate nothing; ... observable<nothing> bus; 

This is as portable as any other type, but is embedded in std so there's some level of standardization.

Comments

0

Boost.MPL offers the type void_.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.