0

I want to try to forward declare a templated class with a default template parameter.

My code looks like this:

 template <typename T = int> class temp;

 int main()
 {
      temp<> def; // error here
 }

 template <typename T>
 class temp
 {
     T val;
 public:
     temp() = default;
     // other stuff can go here, not relevant though
 }

I am working in c++11. Is there any way around this error or am I just stuck?

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
basil
  • 690
  • 2
  • 11
  • 30
  • You can't use a concrete type before it is defined. – NathanOliver Oct 07 '19 at 16:46
  • https://stackoverflow.com/questions/553682/when-can-i-use-a-forward-declaration/33448221#33448221 – R Sahu Oct 07 '19 at 16:53
  • 2
    Not directly realted, but it is going to bite you one day: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Yksisarvinen Oct 07 '19 at 16:54
  • @Yksisarvinen I see this comment often. But I think it is okay for examples and illustrative purposes. – Empty Space Oct 07 '19 at 16:57
  • @TrickorTreat I don't. – Lightness Races in Orbit Oct 07 '19 at 16:58
  • @TrickorTreat Given that there's no single thing that would need these two lines, I assumed that it's just "file default" for OP. And even for minimal reproducible example on SO I'd be afraid to use that, given the vast amount of names residing in namespace std - it's a russian roulette with the compiler. – Yksisarvinen Oct 07 '19 at 17:04
  • @Yksisarvinen I'm well aware, this is not my actual code, just something short to illustrate the point – basil Oct 07 '19 at 17:21
  • Which part of it is required to illustrate the point? – Lightness Races in Orbit Oct 07 '19 at 17:33

0 Answers0