Solved

default value initializer in templatized class

Posted on 2002-04-15
5
246 Views
Last Modified: 2010-04-02
I want to create a templatizable class which includes a default value for the templatized data type.  The following oversimplified example will work for atomic types like int and BOOL but not for data types that are structures or classes:

template <class DataType, DataType DefaultValue>
class CMyClass
{
protected:
  DataType m_Data;
public
  CMyClass() {m_Data = DefaultValue;}
}; //end CMyClass

I get several errors if I try to instantiate the following:
CMyClass<CString, "default string value"> MyObject;

I found that CString is not a valid type for a template argument because structures are not allowed.  MSDN (my C++ documentation) says to use a pointer instead if that is the case, but when I have tried that, I get an error like "expected constant value" because I have to instantiate it something like this:

const CString DefaultValue("default string value")
CMyClass<CString*, &DefaultValue> MyObject;

and &DefaultValue is where it expects a constant value and I can't give it one.  I use CString as an example, but I want to be able to use this on custom classes as well (all of which would support operator= so that the templatized constructor code m_Data = DefaultValue would work) otherwise, there are a variety of alternative solutions I could use just for strings.

--------

I want to be able to make a similar class for arrays like this:

template <class DataType, int nNumElements, const DataType* pDefaultValue>
class CMyArrayClass
{
protected:
  DataType m_arData[nNumElements];
public:
  CMyClass()
  {
    for (int i=0; i<nNumElements; i++)
       m_arData[i] = pDefaultValue[i];
  }
}; //end CMyArrayClass

I have tried the above code using a char array as follows:
CMyArrayClass<char, 4, "test"> MyObject;
but I get an error that "test" is an invalid template argument (even though it is a const char*).  If I use NULL instead, it works, but that doesn't help me because I want to pass in an initial value other than a non-value like NULL.

So what can I do?
0
Comment
Question by:ris
  • 4
5 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6942980
Default values are not allowed as a template value.

Continue.....
0
 
LVL 30

Expert Comment

by:Axter
ID: 6942986
>>So what can I do?

There is a way around this, but it's not a clean method.  In other words, it's ugly.
I'll post an example code in a follow-up comment.

The basic idea would be to create either a singleton class, or a class with a single static function, that would return the default value.
0
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 6943012
Here's an example method.

#include <stdlib.h>

#pragma warning (disable:4786)
#include <string>
using namespace std;


template<typename T>
class DefaultValueTemplateClass
{
public:
     static T DefaultValue();
};

template<class T, class T_DF = DefaultValueTemplateClass<T> >
class foo
{
public:
     foo(const T& Src = T_DF::DefaultValue()) : m_data(Src)
     {
     }
     T m_data;
};


#define MAKE_DEFAULT_TEMPL_VAUE(T,V) T DefaultValueTemplateClass<T>::DefaultValue(){return V;}

MAKE_DEFAULT_TEMPL_VAUE(int,54)
MAKE_DEFAULT_TEMPL_VAUE(string,"Hello World")
MAKE_DEFAULT_TEMPL_VAUE(long,99)

int main(int argc, char* argv[])
{
     foo<int, DefaultValueTemplateClass<int> > foo_int;
     printf("foo_int = %i\n", foo_int.m_data);

     foo<int> foo_int_with_default_default;
     printf("foo_int_with_default_default = %i\n", foo_int_with_default_default.m_data);

     foo<long> foo_long;
     printf("foo_long = %i\n", foo_long.m_data);

     foo<string> foo_string;
     printf("foo_string = %s\n", foo_string.m_data.c_str());


     system("pause");
     return 0;
}

0
 
LVL 30

Expert Comment

by:Axter
ID: 6943054
Correction:
Even though the previous macro I posted, compiles and runs on VC++ 6.0, it shouldn't have.
The correct syntax, is the following:

#define MAKE_DEFAULT_TEMPL_VAUE(T,V) template<> T DefaultValueTemplateClass<T>::DefaultValue(){return V;}
0
 
LVL 1

Author Comment

by:ris
ID: 6943070
I see what you are getting at there.  Thanks.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 72
ASP.net build a IF/Then Walkthrough Guide 1 208
Embarcadero WebBroker REST server endpoint parameters 2 64
Precision Problem in C++ 7 31
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now