Thread: Mixing implicit and explicit template arguments

  1. #1
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174

    Mixing implicit and explicit template arguments

    gcc compiles this code, but I'm a little concerned it's not strictly legal C++:

    Code:
    #include <iostream>                                                                                                                                          
    using namespace std;
    
    template <typename OutType, typename InType>
    OutType StaticCastWrapper(InType x) 
    {
        return static_cast<OutType>(x);
    }
    
    int main()
    {
        cout << StaticCastWrapper<int>(5.3) << endl;
       
        return 0; 
    }
    Anyone know?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    as I understand it, as long as the compiler can tell what the template type is, it doesn't care if you specify it. in your case, you're telling it what the output type is, and it is able to infer what the input type is, so it compiles. you will certainly run into problems if you have 3 or more template parameters and explicitly specify the first and last, and try to have it infer the middle one(s).

  3. #3
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    To me the issue of having an explicit template argument after an implicit one seems only syntactic, just like default function parameters. You'd need to write something like:

    Code:
    #include <iostream>                                                                                                                                          
    using namespace std;
    
    // note the order of template arguments has been swapped
    template <typename InType, typename OutType>
    OutType StaticCastWrapper(InType x) 
    {
        return static_cast<OutType>(x);
    }
    
    int main()
    {
        cout << StaticCastWrapper<, int>(5.3) << endl;
       
        return 0; 
    }
    In the case of default parameters, I believe something analogous to the above is possible in excel vba, and I suppose it would work in C++, but I guess it's just ugly.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    but in C++ that's not valid syntax. I don't know of any situation where you can have a comma after nothing and expect it to compile. default/implicit parameters must always follow explicit parameters, and no explicit parameters may follow default/implicit ones.

  5. #5
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Of course. By "I suppose it would work in C++", I mean I suppose that the C++ language could fairly easily be changed to allow it.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    But then, are there any motivating examples where you'd greatly benefit?

    Because otherwise being able to call a function like

    Code:
    foobar<,,,int,,double,,char>(a, b, c, d);
    isn't much more readable than just

    Code:
    foobar<int, double, char>(a, b, c, d);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by anon View Post
    But then, are there any motivating examples where you'd greatly benefit?
    I doubt it.

    I just had a thought though. If for some reason you wanted a templated function that would have its templates determined implicitly and wanted to use default parameters, you'd want template parameters corresponding to default parameters at the beginning of the template list despite having to be at the end of the function parameters list, which would be confusing.

    Then again, combining implicit templates and default parameters would be pretty confusing regardless of parameter ordering problems, so you'd probably want to find some other way to solve whatever problem it would be.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If you specify explicit template parameters, they are bound to the template variables from left-to-right. Thus, if there are some parameters which should always be specified explicitly, and some which can be implicit, the explicit ones should be listed first. This is exactly the same as calling a function with default arguments...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with implicit and explicit constructor calls
    By mborba22 in forum C++ Programming
    Replies: 1
    Last Post: 09-22-2010, 03:49 PM
  2. Though implementation problem
    By Elysia in forum C++ Programming
    Replies: 296
    Last Post: 05-31-2008, 01:02 PM
  3. Replies: 6
    Last Post: 08-12-2007, 01:02 PM
  4. Implicit and Explicit
    By ripper079 in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2002, 12:22 PM
  5. implicit explicit
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 01-22-2002, 04:10 AM

Tags for this Thread