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

  • Home
  • SEARCH
  • 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 8196321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:26:24+00:00 2026-06-07T05:26:24+00:00

I am trying to achieve a very simple thing using C++ templates. I would

  • 0

I am trying to achieve a very simple thing using C++ templates. I would like to build a generic sign function that could handle the case sgn(x)where x could be either doubleor std::vector<double> (that is returning a std::vector<double> containing the results). In order to achieve that I am using templates

double f(double x) {
    return (x>=0)?1.0:-1.0;
};

template<typename T>
T F(T x) {
   // ?
};

I would like to cast the template and either use f if double or a for loop if std::vector<double>. Unfortunately my function does not use any arithmetic operator and the conditional operator cannot be overloaded. How should I proceed ?

  • 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-06-07T05:26:25+00:00Added an answer on June 7, 2026 at 5:26 am

    While overloading is the best way to achieve what you want, it is not
    really the way to work with the C++ standard library.

    If you want to apply a function to all elements of a container (either
    mutating them or creating new results), use std::transform or
    std::for_each with the function you want to use.

    std::vector<double> doubles, results;
    std::transform(begin(doubles), end(doubles), std::back_inserter(results), sgn);
    

    That separates concern far better than your current approach. Only
    operate on whole containers if it is really required, in all other
    cases use iterators and higher-order functions.

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

Sidebar

Related Questions

I am trying to achieve a very simple thing in MATLAB. I have a
I am trying to do a very simple thing. I want that when I
I am trying to build a very simple C++ program using the Maven NAR
I'm very new to Redmine/Ruby trying to achieve a simple plugin that takes the
I'm new to .NET. I'm trying to do something very simple. I would like
I am trying to achieve a very simple goal, however it does not seem
I am trying to achieve this effect with jQuery UI - very much like
I'm trying to do something that should be very simple, but it's causing this
What I'm trying to achieve is very simple, but a bit difficult to explain:
I have been trying very hard to achieve rounded corners with IE6+jquery ui tabs.

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.