Templates with default types

This is a discussion on Templates with default types within the C++ Programming forums, part of the General Programming Boards category; Consider this structure: Code: template<class T = double> struct Vector { Vector() { x = T(0); y = T(0); } ...

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Templates with default types

    Consider this structure:
    Code:
    template<class T = double>
    struct Vector
    {
      Vector()
      {
        x = T(0);
        y = T(0);
      }
    
      ...
    
      T x;
      T y;
    }
    To create a structure you type:
    Code:
    Vector<float> v1;
    Vector<> v2; //T = double
    However the <> is still needed even when using the default type. From a pedantic and visual standpoint I really find this ugly. Is there any way to make this work:
    Code:
    Vector<float> v1;
    Vector v2; //T = double
    (without typedefing and appending letters like Vectorf, Vectord, Vectori etc...)

    ?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    Nope.

    "Vector<>" is required by the standard. You'll have to settle on a typedef or macro or whatever.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates vs Virtual
    By Dae in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2009, 12:48 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  3. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  5. Help with default parameter value using ptr-to-functor
    By registering in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2004, 03:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21