-1

In my header file I want to avoid using #include but my class will have a vector or a pointer to a vector. I'm content with just a pointer, but I can't figure out how to declare it. Will I have to declare it as a void* and always cast it? That would be lame.

// What do I type here to forward declare vector?

class Counters
{
    Counters();

    void inc(const char* s);

    void print();

    void clear();

private:
    std::vector<int>* Counts;
    int total;
};

Please note carefully that I only want a POINTER TO a container to be stored in my class, NOT a container. The size of pointers is known without needing to refer to the declaration of the container, so please do not answer that this can't be done because the compiler needs to know the container declaration.

All The Rage
  • 743
  • 5
  • 24
  • 2
    *"In my header file I want to avoid using #include"* That's the problem right there, just include what you need. – Baum mit Augen May 27 '16 at 22:51
  • 3
    Why do you want to avoid `#include `? There shouldn't be any harm doing so. – πάντα ῥεῖ May 27 '16 at 22:51
  • Forward declarations exist for a reason, guys. There is tons of literature on pruning #includes to reduce recompilation, improve compile times, improve portability, break circular dependencies, etc. In my case this declaration will be consumed by the CUDA compiler as well as the main C++ compiler and it's well worth trying to avoid including too much crap in code that CUDA will be parsing. Trust me. – All The Rage Jun 07 '16 at 15:56
  • 1
    @Brian, this is NOT a duplicate. I studied the question "Forward declare an STL container?" thoroughly before I posted. There are enormous differences between forward declaring a pointer and a data structure. My case is easy for the compiler; I just need to figure out the syntax of the forward declaration. – All The Rage Jun 07 '16 at 15:58
  • 2
    @AlltheRage I know the question is different, but the answer is the same. You cannot forward declare `std::vector` or other standard library containers, because it's undefined behaviour in most cases to add your own declarations to `namespace std`. – Brian Bi Jun 07 '16 at 18:17
  • _"so please do not answer that this can't be done because the compiler needs to know the container declaration."_ — Yes. That's exactly what the compiler needs to know. A member `T*` requires `T` to be declared. – Emil Laine Jun 07 '16 at 20:57
  • "Trust me" he says. Just accept that it's not really doable, and certainly not worth doing. #include will not hurt your compile times, won't cause unnecessary recompilation (it should never change), won't hurt portability (it's part of the standard!), won't cause circular dependencies, or any of the other parade of horribles you've heard. What you want to do is premature optimization, the root of all evil. – Rob K Jun 07 '16 at 21:06

1 Answers1

0

Maybe this works in your situation:

//header
struct Container;

struct Foo{
    Container *container;
};

//cpp
#include <vector>

struct Container : std::vector<int>{

};

int main(){
    Foo foo;
    foo.container = new Container;
    foo.container->push_back(42);
}
nwp
  • 9,623
  • 5
  • 38
  • 68
  • 3
    Don't inherit from standard containers. http://stackoverflow.com/questions/2034916/is-it-okay-to-inherit-implementation-from-stl-containers-rather-than-delegate The pimpl idiom would otherwise be fine. – Rob K Jun 07 '16 at 21:09