1

This class has an enum:

class ThreadController
{
public:
  enum ThreadType { ... }
}

Is it possible to use ThreadType & from the forward declared class?

class ThreadController;

class ThreadWorker
{
public:
  static ThreadWorker makeThreadWorker(const ThreadController::ThreadType & type);
}

I get the following error:

'ThreadType' in 'class ThreadController' does not name a type

But since I am using a reference, can't the compiler be happy with not having a definition in the header file?

  • The compiler doesn't know anything about what `ThreadController` contains at that point. – chris Aug 30 '14 at 02:26
  • How do you tell the compiler? A nice use of `typename`? –  Aug 30 '14 at 02:27
  • possible duplicate of [Forward declaration of nested types/classes in C++](http://stackoverflow.com/questions/951234/forward-declaration-of-nested-types-classes-in-c) – ikh Aug 30 '14 at 02:30
  • @MiyazawaKenji, It needs the definition. It can't possibly gather anything from just a declaration. – chris Aug 30 '14 at 02:31
  • I thought it didn't need a definition since the reference type is just an 8-byte pointer (on x86-64). –  Aug 30 '14 at 02:32
  • @chris That's a language limitation though - "can't possibly" is taking it too far. Allowing forward declarations of nested entities would be one possible way around it, for example. – JBentley Aug 30 '14 at 02:36
  • 1
    Can you reverse the two? Forward declare `ThreadWorker` instead. –  Aug 30 '14 at 02:39

1 Answers1

1

You could make makeThreadWorker a templated function.

template <typename T = ThreadController>
static ThreadWorker makeThreadWorker(const typename T::ThreadType & type)
{

}

The compiler will throw an error if T doesn't contain ThreadType. Optionally add a static_assert to restrict T to ThreadController only.

static_assert(std::is_same<ThreadController, T>::value, "error");