Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 20th, 2005, 2:56 PM   #1
earl
Newbie
 
Join Date: Jun 2005
Posts: 18
Rep Power: 0 earl is on a distinguished road
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.
earl is offline   Reply With Quote
Old Jun 20th, 2005, 3:12 PM   #2
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
Something like this:

struct Foo
{
};

template<class _Ty = Foo>
struct Bar
{
};

works fine.
Animatronic is offline   Reply With Quote
Old Jun 20th, 2005, 6:02 PM   #3
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
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.
uman is offline   Reply With Quote
Old Jun 20th, 2005, 7:48 PM   #4
earl
Newbie
 
Join Date: Jun 2005
Posts: 18
Rep Power: 0 earl is on a distinguished road
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...
earl is offline   Reply With Quote
Old Jun 21st, 2005, 12:27 PM   #5
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>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.
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC