Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5963671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:17:42+00:00 2026-05-22T19:17:42+00:00

I have this template function: template <class P> double Determinant(const P & a, const

  • 0

I have this template function:

template <class P>
double Determinant(const P & a, const P & b, const P & c) {
    return  (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);
}

but I want to avoid forcing the return type to double all the time — P::x and P::y could be ints too, and I need this function in both situations. Is there a way to specify the type of x and y, something like this?

//doesn't compile; can't deduce template argument for T
template <typename T, class P>
T Determinant(const P & a, const P & b, const P & c) {
    return  (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);
}

edit: My compiler is VC2005

edit2: sorry to forget to mention: Unfortunately I can’t modify the implementation of the structs for P; one of the point types I deal with is MFC/ATL’s CPoint, which are hard-coded as { long x; long y; }.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:17:43+00:00Added an answer on May 22, 2026 at 7:17 pm

    Compiler cannot deduce return-type of function template, from function argument. Type deduction is done with function arguments only.

    In C++03, you can define typedef in your class as:

    struct A //suppose A is going to be type argument to your function template
    {
       int x, y;     //I'm assuming type of x and y is same!
       typedef int value_type; //type of x and y!
    };
    

    And then you’ve to re-write your function as:

    template <class P>
    typename P::value_type Determinant(const P & a, const P & b, const P & c) {
        return  (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);
    }
    

    Notice the return-type now, its a dependent type. Its :

    typename P::value_type
    

    The keyword typename is required here.


    Alright, as you said you can’t modify your structs, then you can use traits instead. Here is how this can be done:

    template<typename T> struct PTraits;
    
    //Suppose this is your type which you can't modify
    struct A //A is going to be type argument to your function template
    {
       long x, y; 
    };
    
    //specialization: defining traits for struct A
    template<>
    struct PTraits<A>
    {
        typedef long value_type; //since type of A::x and A::y is long!
    };
    

    And your function template would look like this:

    template <class P>
    typename PTraits<P>::value_type Determinant(const P & a, const P & b, const P & c) {
        return  (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);
    }
    

    Notice the return-type; its slightly different now:

    typename PTraits<P>::value_type
    

    Again, value_type is a dependent name, so the keyword typename is required.

    Note that you’ve to specialize PTraits<> for each type which you pass to the function template, as I did.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume we have a template function foo: template<class T> void foo(T arg) {
I have a 2884765579 bytes file. This is double checked with this function, that
I have trouble compiling a class, which has function pointers as member variables. The
i'm lost in this , i have a class that has three vector objects
I'm trying to create a template class, and when I define a non-member template
An explicit instantiation of a static template member function keeps failing to compile with
I have defined a class in C++ which holds an array of scalars of
I have a bunch of overloaded functions that operate on certain data types such
The below code compiles with gcc v4.3.3 and the templated child class seems to
I'm in the process of writing a smart pointer countedptr and I've hit a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.