template declarators

This is a discussion on template declarators within the C++ Programming forums, part of the General Programming Boards category; given: Code: struct A { } a; how does this work with templates?...

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699

    template declarators

    given:

    Code:
    struct A {
    
     } a;
    how does this work with templates?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    temaplate<class T>
    struct A
    {
      T data;
    };
         
    int main( )
    {
    
    A<int> a;    //You can change the data type to basically anything you want here at object creation
                 //It will change the type qualifier for the 'data' struct variable
    
    return 0;
    
    }
    Last edited by The Brain; 12-19-2004 at 05:31 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Heh, well you could aways try testing it Some templates actually require a constructor and destructor to compile. Other than that, I believe an empty structure takes up one byte of space (please someone jump in with a correction).

    [edit]
    I guess I misunderstood what you meant.
    [/edit]
    Last edited by master5001; 12-19-2004 at 05:31 PM.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    I suppose I'm being too vague here.

    consider:

    Code:
    typedef struct foo { } bar, baz;
    
    foo f;
    bar r;
    baz z;
    I want to use the same syntax with templates.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    template <typename T>
    class Foo {
      T x_;
      public:
        Foo(){};
        const T& x() const { return x_; }
    };
    
    typedef Foo<int> FooInt;
    
    FooInt Fi;

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    ok, here's what I was hoping for:

    Code:
    template <class T> typedef struct foo { } bar, baz;
    
    foo <int> f;
    bar <int> r;
    baz <int> z;
    in other words, typedef the template itself - not a particular parameterization of the template.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well thats done by default:

    Code:
    template <typename T> struct foo { /* stuff */};
    foo<int> f;
    Unless do you also want to be able to call foo bar and baz?

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well you can do this..

    Example:
    Code:
    template <typename T>
    struct foo {
    
    };
    
    typedef foo<int> bar;
    But if you are just wanting foo and bar and baz to be the same structure, why not use macros? You will lose compile time information since the compiler will see bar and baz as just foo in disguise. I'm really not sure why you are wanting to do this.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> But if you are just wanting foo and bar and baz to be the same structure, why not use macros?

    I'd rather not use macros at all - I was just hoping there was some obscure feature of the language that would allow you to do this.

    >> I'm really not sure why you are wanting to do this.

    why does anyone want to typedef anything? maybe because it's easier to type?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can't have typedef templates, so what you're trying to do is impossible. Sorry. Basically, in order to allow your idea, this would need to be possible:
    Code:
    template <typename T> typedef vector<T> dynarray<T>;
    Admittedly, that would allow cool stuff:
    Code:
    template <typename T> typedef vector<vector<vector<T> > > vec3d<T>;
    But it doesn't exist. It is being discussed as a language extension, but there is some weird stuff that needs to be resolved, especially when it comes to function parameter deduction.
    Code:
    template <typename T> typedef T CT<T>;
    
    template <typename T>
    void whee(CT<T> arg);
    It's very hard to correctly deduct the type here.

    As a workaround for now, you can use nested typedefs:
    Code:
    template <typename T>
    struct vec3d
    {
      typedef vector<vector<vector<T> > > t;
    };
    
    vec3d<int>::t myvec;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I never put much thought into it, but yeah it would be handy. I have had instances where I've had to ABCs that are fundamentally the same template, but for the sake of neatness, they had different names. But even then I can't remember when i've made to mirror templates before. But perhaps thats because nothing like you are suggesting here exists

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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