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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:34:32+00:00 2026-05-10T19:34:32+00:00

This article describes a way, in C#, to allow the addition of arbitrary value

  • 0

This article describes a way, in C#, to allow the addition of arbitrary value types which have a + operator defined for them. In essence it allows the following code:

public T Add(T val1, T val2) {    return val1 + val2; } 

This code does not compile as there is no guarantee that the T type has a definition for the ‘+’ operator, but the effect is achieved with code like this:

public T Add(T val1, T val2) {    //Num<T> defines a '+' operation which returns a value of type T    return (new Num<T>(val1) + new Num<T>(val2)); } 

Follow the link to see how the Num class achieves this. Anyways, on to the question. Is there any way to achieve the same effect in C or C++? For the curious, the problem I’m trying to solve is to allow a CUDA kernel to be more flexible/general by allowing it to operate on more types.

Update: For .NET, Marc Gravell has made a utility library which solves the operator problem very elegantly.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T19:34:33+00:00Added an answer on May 10, 2026 at 7:34 pm

    Due to the way templates are compiled in C++, simply doing:

    template < class T > T add(T const & val1, T const & val2) {     return val1 + val2; } 

    will work, you’ll get a compile error for every type where an operator+ is not defined.

    C++ templates generate code for every type instantiation, so for every type T code will be generated that does the right thing. This way C++ doesn’t need Num<> trickery.

    In plain C, this is not possible as far as I know.

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

Sidebar

Related Questions

I have been reading this article: Many ways to communicate with your database using
I have been asked this question, but failed to find an article that describe
This article describes a great pattern called 'Domain Events': http://www.udidahan.com/2009/06/14/domain-events-salvation/ One major flaw with
This Wiki article: Adding a Product to the Cart via Querystring describes how one
I was wondering if anyone knows about a good article which describes dividing workload
I just read this article on tdwtf.com . Generally, it describes an archiving bot
For some reason, addAsync chaining in a flexunit test as described in this article
This article gives a good overview on why structured exception handling is bad. Is
This article here suggests to use -XX:+UseParNewGC To enable a parallel young generation GC
This article states that If your site is run on a shared Web server,

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.