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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:37:47+00:00 2026-06-04T18:37:47+00:00

I have various classes in a C++0x framework and would like to write functions

  • 0

I have various classes in a C++0x framework and would like to write functions to convert between some of them. For example:

struct Foo { float v; };
struct Bar { int i; };
struct Tar { float p, q; };

void Convert(const Foo& x, Bar& y) { y.i = static_cast<int>(x.v); }
void Convert(const Tar& x, Foo& y) { y.v = x.p + x.q; }

This is just an example. There are quite a lot of “small” classes. And not all conversion functions make sense.

Additionally there are some classes which essentially behave like STL containers and should “inherit” these conversion functions.

void Convert(const std::vector<Foo>& cx, std::vector<Bar>& cy) { ... }
void Convert(const std::vector<Tar>& cx, std::vector<Bar>& cy) { ... }

No I am looking for an easy way to define theses functions. I tried:

template<typename X, typename Y>
void Convert(const std::vector<X>& cx, std::vector<Y>& cy) {
   cy.resize(cx.size());
   for(std::size_t i=0; i<cx.size(); i++) {
     Convert(cx[i], cy[i]);
   }
}

and this works perfectly.

However with such a setup one has to write

std::vector<X> cx = { ... };
std::vector<Y> cy;
Convert(cx, cy);
// when not specifying the type, one needs to use this form f(X, &Y)

With a setup like

template<typename X, typename Y>
std::vector<Y> Convert(const std::vector<X>& cx) {
   std::vector<Y> cy(cx.size());
   for(std::size_t i=0; i<cx.size(); i++) {
     cy[i] = Convert(cx[i]);
   }
   return cy;
}

one has to write

std::vector<X> cx = { ... };
std::vector<Y> cy = Convert<X,Y>(cx);
// can I avoid specifying the source type with this form?

Of course at some point the target type need to be mentioned, but the source type is defined by the function parameter. I do not want to mention it again and again.

Is there a elegant generic way to handle such conversion functions?

Edited the question for clarification

  • 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-04T18:37:49+00:00Added an answer on June 4, 2026 at 6:37 pm

    Actually I found out how to do this. By re-ordering the template arguments

    template<typename Y, typename X>
    std::vector<Y> ConvertTo(const std::vector<X>& cx) {
       std::vector<Y> cy(cx.size());
       for(std::size_t i=0; i<cx.size(); i++) {
         cy[i] = Convert(cx[i]);
       }
       return cy;
    }
    

    one can write

    std::vector<Y> cy = ConvertTo<Y>(cx);
    

    and the compiler automatically infers the second template type for input. Nice feature!

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

Sidebar

Related Questions

I'd like to create various sorting classes (QuickSort, MergeSort, BucketSort...etc). I have a common
I have various classes which all contain address details, i.e. AddressLine1, AddressLine2, AddressLine3, Suburb,
I have a wxPython application with the various GUI classes in their own modules
I have various time-series I'd like to correlate and present as either a csv-file
I have various settings in my ccnet.config file and wish to pass them to
im using mvc framework and i have learned some techniques that help me with
I have a couple of classes (for this example anyway) that use code first
I have various classes Class1 , Class2 , Class3 , etc. all of which
I'm presently attempting to write a few classes to aid me in writing various
I have a business model with many classes in, some logical entities within this

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.