tags:

views:

93

answers:

4

My original code looks like this:

class a{
...
char buff[10];
}

and im attempting to make this change to the code:

template <int N = 10>
class a{
...
char buff[N];
}

Is there any thing I can do to keep my existing code creating instances of class a like this:

a test;

instead of making the change to:

a<> test;

to get the default parameter?

A: 

Nope. The empty angle brackets are required.

eduffy
+7  A: 

You can't instantiate a template without angle-brackets, and you can't give a type the same name as a template, so you can't do exactly what you want.

You could give the template a different name, and typedef a to the default-sized one.

Mike Seymour
+1  A: 

Well, don't make the class a template is the obvious answer - use something like:

class a {
   public:
      a( int n = 10 ) : buff(n) {}
   private:
      std::vector <char> buff;
};
anon
Not really the same behaviour.
DeadMG
@DeadMG it is the same observed behaviour, which is all that matters in most cases.
anon
+1  A: 

Not in really good ways. You can typedef X to be X<> in a different namespace:

namespace lib {
template<int N=10>
struct X
{
 int t[N]; 
};
}
typedef lib::X<> X;
int main()
{
 X a;
 lib::X<20> b;
}

-- or --

template<int N=10>
struct X
{
 int t[N]; 
};
int main()
{
 typedef X<> X; // functions have their own namespace!
 X a;
 ::X<20> b;
}
Luther Blissett