2

often I encounter hacks like

//lets say this is some class that still doesnt support...
//...all the functionality that it should based on the design docs
void MyClass::MyFunction()
{
  throw std::exception("not implemented");
}

I guess this is a bad practice, but that aside:
Is there a way to do the same thing during compilation, but only if the function is used (aka if it is unused compile should succeed).

Edit: Im also interested in virtual mem functions.

1
  • If a function is not implemented (as in, not declared but not defined) then you will get a link time error. In this case, your function is implemented, and has well defined behavior. Commented Nov 28, 2012 at 16:00

4 Answers 4

6

If it is non-virtual function then you can simply comment out the definition.

If it is a virtual function declared in a base class, then you can't control the calls at compile time, so then your only option is some run-time error or exception.

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

Comments

6

If you remove the implementation entirely and only have the function declaration, there will be a linker error, which is basically compile time. Unfortunately, linker errors have a tendency to be ugly and hard to track down, but in the case of calling a function that hasn't been implemented yet I think they're pretty manageable.

2 Comments

Linker error is not basically compile-time. It is link-time.
What if the function is virtual and some base class has implementation?
2

Old question, but still...

I use a couple of simple helper for this. It'll give an error link-time that's fairly readable:

// Not implemented is not implemented :-)thing, it'll break:
struct NotImplHelper { static void notimplemented(); };
#define notimplemented() NotImplHelper::notimplemented();
#if defined(DEBUG) || defined(_DEBUG)
#define notimplementedvirtual() throw std::exception();
#else
#define notimplementedvirtual() static_assert(false, "You should implement virtual function calls before moving to production.");
#endif

Usage:

//lets say this is some class that still doesnt support...
//...all the functionality that it should based on the design docs
void MyClass::MyFunction()
{
    notimplemented();
    // or notimplementedvirtual() if MyFunction() is virtual... 
}

Rationale:

IMHO if you use a function in your program, it should be available. When you try to compile something that you haven't implemented yet, it should give a compile-time or link-time error.

F.ex., in MSVC++ this'll give:

1>Test.obj : error LNK2019: unresolved external symbol "public: static void __cdecl NotImplHelper::notimplemented(void)" (?notimplemented@NotImplHelper@@SAXXZ) referenced in function "[blahblahblah]"

Note that the 'referenced function' is there in MSVC++. I haven't tested it in other compilers.

As for not-implemented virtual function calls, the only option you have it to throw an exception. Not having these implemented in your debugger while developing is fine - however, the moment stuff gets serious, these might be called by your program, so they should be available. A static_assert ensures the latter. (So: combined with any continuous integration package, it'll basically fail.)

Obviously most people will accidentally mix up notimplemented and notimplementedvirtual. In reality this ain't a big problem: a simple solution is to always use the former, unless you want to get rid of the error because it's a WIP.

Comments

0

The simplest solution I can think of is to comment the unimplemented function.

Maybe not what you had in mind, but doing so will generate a compile-time error if anything tries to use it, and the resulting code should be identical to an empty function, which is usually optimized away.

Comments

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.