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 cha开发者_StackOverflownge to:
a<> test;
to get the default parameter?
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.
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;
};
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;
}
Nope. The empty angle brackets are required.
精彩评论