C++ templates without arguments
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: C++ templates without arguments

  1. #1
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    C++ templates without arguments

    Some reading through C++ code I come across template deceleration without any type , I have tested the code it compiles too
    Code:
    template<>
    class abc
    {
    
    }
    similarly template usage
    Code:
    abc<> a;
    could some one please why this is so.

    example of such usage can be found here
    http://www.boost.org/doc/libs/1_39_0/libs/random/
    Last edited by aamir121a; January 23rd, 2013 at 03:36 AM.

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: C++ templates without arguments

    Quote Originally Posted by aamir121a View Post
    Some reading through C++ code I come across template deceleration without any type , I have tested the code it compiles too
    Code:
    template<>
    class abc
    {
    
    }
    It doesn't for me.

    explicit specialization; 'abc' is not a specialization of a class template
    This does compile. Are you sure you didn't mean this?

    Code:
    template<typename T = int>
    class abc
    {
    
    };
    
    int main()
    {
        abc<> x;
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: C++ templates without arguments

    I am using VS 2008 , however you are right when I cleaned the project and rebuild it does not work. ( however your code works fine ) however I still don't understand why template are being used this way , to my understanding the whole idea of template is create a generic framework which can be applied to any data type.

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

    Re: C++ templates without arguments

    Quote Originally Posted by aamir121a View Post
    ... which can be applied to any data type.
    This isn't entirely true.
    Templates are not required to work for "any type", they are work for any type that is compatible with what the templated is designed for.
    The compiler will "try" to substitute whatever type you provide as template parameter, but this can very much result in a compiler error if this type doesn't conform to what the template expects.

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: C++ templates without arguments

    to my understanding the whole idea of template is create a generic framework which can be applied to any data type.
    The template parameter doesn't even have to be a type.

    Here's a template with a non-type parameter, with defaults.
    Code:
    template <const size_t SIZE = 1024>
    class FixedLengthContainer
    {
    
    };
    
    FixedLengthContainer<> container1;        // Create a default 1K sized object.
    
    FixedLengthContainer<1048576> container2; // Create a 1M sized object.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #6
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: C++ templates without arguments

    Thank you john and OReubens

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center