template class and string default value
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: template class and string default value

  1. #1
    Join Date
    Feb 2007
    Posts
    102

    template class and string default value

    Hi,

    I would like to define a templated class while implementing default value on templated arguments. I don't know how to do that with string templated variables.

    For exemple:

    Code:
    template <class T>
    class A {
    public:
    
        A() { version = ???? }
    
        std::string_base<T> version;
    
    };
    I don't want to pass the default value as parameter of the constructor.
    Does someone know how I can do this?

    Thank you,

    madric

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    1,999

    Re: template class and string default value

    Quote Originally Posted by madric View Post
    I would like to define a templated class while implementing default value on templated arguments. I don't know how to do that with string templated variables.
    Do you want a default type for the template argument or a default value for your member variable?
    The first goes like this
    Code:
    template <class T = int>
    class MyClass
    {
    //...
    };
    MyClass<> def;
    In the second case, it all depends on what type you require the template argument to be and what kind of default value you want to assign. If it's up to the user of the class, you can consider adding a function object template argument or using a traits class.
    (I did not compile this code).
    Code:
    // using predicate
    template <class T, class Initializer>
    struct Foo
    {
        Foo(const Initializer& init = Initializer()) : m(init()) {}
        T m;
    };
    
    // using traits
    template <class T>
    struct BarTraits<T>
    {
        static T init() { return 0; }
    };
    template <class T, class Traits = BarTraits<T> >
    struct Bar
    {
        Bar() : m(Traits::init()) {}
        T m;
    };
    Last edited by D_Drmmr; October 24th, 2013 at 01:50 AM.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Oct 2013
    Posts
    5

    Re: template class and string default value

    I may have misunderstood but are you looking for different default values per type? If so, this could be achieved by implementing the constructor for every type you intend to use with this class:

    Code:
    template <class T>
    class A {
    public:
    
        A(); // Do not implement here
    };
    
    A<int>::A()
    {
        // Int specific set up here
    }
    
    A<float>::A()
    {
        // float specific set up here
    }
    Apologies if I'm barking up the wrong tree and good luck

    Talaxy

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,643

    Re: template class and string default value

    typically speaking... you can provide default values for POD types.
    strings are not PODs
    so you can't make a default string-value.

    there's ways around it (like using traits), but they'll be a bit more involved to implement.
    A simple solution would be to extend your class to need an extra template parameter
    the 2nd parameter can be any class that implements a
    static std::string strvalue()
    and where you need to initialisa your string, you call that function from the 2Nd class.

    Code:
    template <class T, class S>
    class A {
    public:
    
        A() { version = S::strvalue(); }
    
        std::string_base<T> version;
    
    };
    with

    Code:
    class Version1
    {
    public:
       static std::string strvalue()  { return "Version 1.0.0 Beta Super Pro Plusplus with Sugar on top"; }
    };
    and called as
    Code:
    int main()
    {
    A<int, Version1> myA;
    
    }

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    1,999

    Re: template class and string default value

    Quote Originally Posted by Talaxy View Post
    I may have misunderstood but are you looking for different default values per type? If so, this could be achieved by implementing the constructor for every type you intend to use with this class:

    Code:
    template <class T>
    class A {
    public:
    
        A(); // Do not implement here
    };
    
    A<int>::A()
    {
        // Int specific set up here
    }
    
    A<float>::A()
    {
        // float specific set up here
    }
    I'm not sure if this is allowed in C++11, but in C++03 you cannot specialize a member function of a template class; you can only specialize the whole class. VC++ allows this, though.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Oct 2013
    Posts
    5

    Re: template class and string default value

    Ah I see, I might be best sticking to the Visual C++ section then

    Thanks!

  7. #7
    Join Date
    Feb 2007
    Posts
    102

    Re: template class and string default value

    Thank you very much for all your answers.
    I will study the traits class, I am not very familiar with it but I feel that this is my best option.

    Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center