![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 18
Rep Power: 0
![]() |
nontype default template parameters
Is it possible to have a nontype default template parameter that is not an int or a pointer? Every example I see uses the integer example to initialize something like a Stack or Queue template.
For instance, is it possible to specify a nontype template parameter that is a struct? I was trying this but getting errors indicating that the struct wasn't a valid as a non-type parameter, but I'm not sure if that's a language restriction or requires compiler flags...? Thanks. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
Something like this:
struct Foo
{
};
template<class _Ty = Foo>
struct Bar
{
};works fine. |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
I think he means
template<std::string whatever = std::string("foo")> struct zooglemcdorfnick { //w/e }; I'm not sure if you can do that, I don't use templates a lot. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2005
Posts: 18
Rep Power: 0
![]() |
uman is correct. I understand how type parameters work, along with template specializations, etc.
But you can also do something like this: template<class T, int size = 10>
class Stack
{
...
T array[size];
...
};But I'm wondering if it's possible to do something like this: template<class T, Obj o = x>
class Foo
{
};where Obj is some user-defined type and x is an instance. I guess you can't, but you can use pointers/references, i.e. template<class T, Obj *o>
class Foo
{
};But actually I'm not sure if you can specify default parameters for pointer/reference parameters... |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
>Is it possible to have a nontype default template parameter that is not an int or a pointer?
Your choices for non-type template parameters are limited to an integral or enum type, a pointer to object or function, a reference to object or function, and a pointer to a member. All of them allow default arguments. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|