BearFarmWrangler
BearFarmWrangler

Reputation: 13

Is there anyway to create a non-type template parameter for a class but not declare using <>?

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?

Upvotes: 1

Views: 152

Answers (4)

Nordic Mainframe
Nordic Mainframe

Reputation: 28737

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;
}

Upvotes: 1

anon
anon

Reputation:

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;
};

Upvotes: 1

Mike Seymour
Mike Seymour

Reputation: 254431

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.

Upvotes: 7

eduffy
eduffy

Reputation: 40224

Nope. The empty angle brackets are required.

Upvotes: -1

Related Questions