CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    Template parameters and arguments

    I have several questions while reading "C++ Templates: The Complete Guide/David Vandevoorde, Nicolai M. Josuttis"

    Code:
    template<int I> void f(int (&)[24/(4+I)]); 
    
    int main() 
    { 
        &f<4>;  
    }
    1. What is that type: int(&)[3] ? is it reference to a fix sized array of 3 integers?
    2. what is the meaning/result of the statement in main
    and how do I call function f (assuming I'd add an implementation to the template fuction decleration)

    I failed to compile it on Dev-cpp 4.9.9.2 : gcc 3.4.2
    though succeded with Comeau 4.3.3 online compiler -
    which leads me to another question - what is a recommended C++ compiler
    that supports the most of templates features?
    **** **** **** **** **/**

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Template parameters and arguments

    Quote Originally Posted by Guysl
    Code:
    template<int I> void f(int (&)[24/(4+I)]); 
    
    int main() 
    { 
        &f<4>;  
    }
    1. What is that type: int(&)[3] ? is it reference to a fix sized array of 3 integers?
    2. what is the meaning/result of the statement in main
    and how do I call function f (assuming I'd add an implementation to the template fuction decleration)
    1. No idea.
    2. It takes the address of an instance of the template function, thus forcing the compiler to create a function f that takes an int(&)[3] as argument.
    I failed to compile it on Dev-cpp 4.9.9.2 : gcc 3.4.2
    though succeded with Comeau 4.3.3 online compiler -
    which leads me to another question - what is a recommended C++ compiler
    that supports the most of templates features?
    Comeau is afaik known to be the compiler closest to the C++ standard.

  3. #3
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720

    Re: Template parameters and arguments

    Seems the authors failed to express an useful example of language features.

  4. #4
    Join Date
    Jun 2002
    Posts
    1,417

    Re: Template parameters and arguments

    when compiled with Comeau's online compiler, got this message
    Code:
    Comeau C/C++ 4.3.3 (Aug  6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
    Copyright 1988-2003 Comeau Computing.  All rights reserved.
    MODE:strict errors C++
    
    "ComeauTest.c", line 5: warning: expression has no effect
          &f<4>;  
          ^

  5. #5
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Template parameters and arguments

    Quote Originally Posted by Guysl
    1. What is that type: int(&)[3] ? is it reference to a fix sized array of 3 integers?
    Yes, it is a reference to a an array of 3 integers.
    Quote Originally Posted by Guysl
    2. what is the meaning/result of the statement in main
    Basically, it has no effects.
    It is the expression of the address of a template instance of the template function f.
    This template instance has the I parameter equal to int.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured