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 564301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:45:07+00:00 2026-05-13T12:45:07+00:00

What I mean is the following. I want a template function that takes two

  • 0

What I mean is the following. I want a template function that takes two vector iterators (or two pointers to array of double) and returns a double that is somehow related to the vector iterators or array pointers that I pass. However, I want this to work for double or int, or any arithmetic type.

I think I’m not allowed to say:

template <class T> 
T* func(T Begin, T End)

 T new_variable = Begin + 5;

 return (*new_variable);
}

because the compiler won’t understand what T* means. A solution I thought of is to take what I’m trying to return and make it a third argument:

template <class T> 
void func(T Begin, T End, T* new_variable)

 new_variable = Begin + 5;

 return (*new_variable);
}

Will this work? Even if so, is there another way of doing what I’m trying to do? (Sorry if I haven’t been clear enough.)

  • 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-13T12:45:07+00:00Added an answer on May 13, 2026 at 12:45 pm

    If you want to return a double (i.e the type that you would get when dereferencing), you can use the iterator traits:

    template<typename RandomAccessIterator>
    typename std::iterator_traits<RandomAccessIterator>::value_type 
    func(RandomAccessIterator a, RandomAccessIterator b) {
        typedef typename std::iterator_traits<RandomAccessIterator>::value_type 
          value_type;
    
        // use value_type now, when you want to save some temporary
        // value into a local variable, for instance
        value_type t = value_type();
        for(; a != b; ++a) t += *a;
        return t;
    }
    

    These work for all iterators, including pointers:

    int main() {
      int d[3] = { 1, 2, 3 };
      assert(func(d, d + 3) == 6);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following formula float mean = (r+b+g)/3/255.0f; I want to speed it
What does the following mean? Class.Function(variable := 1 + 1) What is this operator
I want to know, what does the following CSS mean? .twoCol #sidebar Isn't #sidebar
I have the following URL: http://somedomain.com/aa/search/search.php I want it to return 2 selections, that
I want to get json with php encode function like the following <?php require
Possible Duplicate: What does this mean? (function (x,y)){…}){a,b); in JavaScript I have the following
What do the following mean in an sql sytax: (+) after a condition eg:
What does the following error mean? Geeneration of designer file failed: Exception from HRESULT:
What does the colon mean in the following Perl program? MAIN: { print Hello\n;
What does the underscore mean in the following regex? [a-zA-Z0-9_] The _ seems to

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.