9

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.

10
  • 3
    if you have access to <type_traits> either from TR1 or by compiler extension, it has the simple solution. Also the compiler may have a related intrinsic function. (i.e. VS2010 does)
    – Balog Pal
    Commented Jun 6, 2013 at 15:58
  • and I lost you somewhere, isn't adding ! in the quoted expression what you're after?
    – Balog Pal
    Commented Jun 6, 2013 at 16:00
  • @BalogPal: As far as I can tell from the code, these assertions don't work by returning a truth value for any part of the parenthesized expression. Instead there is a lot of macro magic involved, but it boils down to code for some function which will not compile unless the concept in question holds. Turning code which does not compile into compiling one and vice versa is tricky at least.
    – MvG
    Commented Jun 6, 2013 at 16:10
  • No type_traits, unfortunately. Strictly C++03.
    – Janoma
    Commented Jun 6, 2013 at 16:13
  • 2
    Again: I can't use C++11, so I can't use is_constructible. Is it that hard to actually read the question?
    – Janoma
    Commented Jun 7, 2013 at 13:17

1 Answer 1

2

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.

3
  • 1
    ... because a type which has more features than those strictly required to satisfy a concept, does satisfy that concept.
    – migle
    Commented Jun 6, 2013 at 16:47
  • Nope, not coding style. I'm using templates, and the actual type will be given externally, so I need a way of checking certain things. The reason to avoid default constructors is by design, and conscious :)
    – Janoma
    Commented Jun 6, 2013 at 16:54
  • 1
    Anyway! If you can actually write a piece of code which does not compile if the type they give you has a default constructor: then you can write a concept check... For instance, the concept check for a default constructor is a piece of code which simply assigns the default constructor to a function pointer... If you cannot write a piece of code which does not compile if the type has a default constructor, then it is not trully a concept check.
    – migle
    Commented Jun 6, 2013 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.