Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I check that a class is *not* default constructible?

Tags:

c++

boost

c++03

First of all, note that I'm using C++03 (and C++11 is not an option). I'm using boost concept to check that a certain class is default-constructible:

BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<my_class>));

However, for some other class I'd like to assert that the type does not have a default constructor. Is there a way of doing this?

Update: to all those super-duper experts marking the question as duplicate or already answered without reading it: I state in the very first paragraph that I already use boost concept to check that classes are default-constructible (which is the question this is supposed to be a duplicate of). I also explicitly state that I can't use C++11, so type_traits are not available to me. So, could somebody please point me to the specific part where my question was "already answered"? Because I haven't found it yet.

like image 584
Janoma Avatar asked Jun 06 '13 15:06

Janoma


People also ask

Is trivially default constructible?

A trivially default constructible type is a type which can be trivially constructed without arguments or initialization values, either cv-qualified or not. This includes scalar types, trivially default constructible classes and arrays of such types.

How do you test a default constructor?

To test that a constructor does its job (of making the class invariant true), you have to first use the constructor in creating a new object and then test that every field of the object has the correct value. Yes, you need need an assertEquals call for each field.

What happens if there is no default constructor?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

Can we use default arguments with constructor?

A default constructor can either have no parameters or parameters with default arguments.


1 Answers

The disappointing bit is that no, this is not possible with boost concept check.

The not so disappointing bit is: aren't you trying to use this tool backwards?

Generally, you write code that needs a type that has a certain number of features, such as constructors, functions that operate on that type, and so on. I can't imagine a situation where you would write code that needs a type which lacks specific features.

You seem not to be wanting to do concept-oriented programming, but to enforce coding style. And this is not the right tool for it.

like image 98
migle Avatar answered Oct 22 '22 16:10

migle