6

I am writing a test driver for a type this is explicitly supposed to not be default constructable. Is there any way to assert in my test driver that this is the case? I can verify manually via compilation errors, but I want something that will protect against future changes that may misguidedly add a default constructor.

Edit: I'm stuck in an environment with C++03. Keeping that in mind, are there any other options than is_default_constructable?

GBleaney
  • 2,096
  • 2
  • 22
  • 40

1 Answers1

11

You can use static_assert(!std::is_default_constructible<T>::value, "Boo");. Make sure to #include <type_traits>.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Makes me really wish I had access to C++11 :(. Is there any way to do this in C++03? – GBleaney Jul 08 '14 at 18:54
  • @GBleaney: Hm, maybe there's some kind of SFINAE trick with defaulted arguments that might work. Let me try. – Kerrek SB Jul 08 '14 at 18:59
  • @GBleaney: Try [this question](http://stackoverflow.com/questions/2733377/is-there-a-way-to-test-whether-a-c-class-has-a-default-constructor-other-than/2770326). – Kerrek SB Jul 08 '14 at 19:08