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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:23:19+00:00 2026-05-31T18:23:19+00:00

UPDATE: I went for the template approach cause it seemed the most elegant /

  • 0

UPDATE:

I went for the template approach cause it seemed the most elegant / concise but then I end up with stuff like this:

template<typename charType>
    int doSomethingWithString(const charType* buffer)
    {
        wchar_t* tempBuffer = NULL;

        if(typeid(charType) == typeid(char))
        {
            tempBuffer = (wchar_t*)malloc(sizeof(wchar_t)*strlen((char*)buffer));
            mbstowcs(tempBuffer, (const char*)buffer, strlen((const char*)buffer));
        }
        else if(typeid(charType) == typeid(wchar_t))
        {   tempBuffer = (wchar_t*)malloc(sizeof(wchar_t)*strlen((char*)buffer));
            tempBuffer = wcscpy(tempBuffer, (const wchar_t*)buffer);
        }

At which point I feel it’s kind of ugly (specially since I still have to have all those casts there to let the compiler know). I also tried turning the parameter into a wstring but I just don’t seem to find an appropriate constructor for both cases?

It’s been a while now that I’ve been away from C++ and I can’t remember the C++ way of going about this:

Let’s say I have a class with some method myClass::doSomethingWithString(…) and I want the same method to be able to be called passing in a character sequence as either const char* or const wchar_t*. Was there a way to make the method be able to accept both types of “strings” and tell if the parameter is const char* or const wchar_t* inside the method implementation? Or is method overloading the only way to go about it?

  • 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-31T18:23:20+00:00Added an answer on May 31, 2026 at 6:23 pm

    Method overloading is the only way to go about it.
    Alternately, You could write a single template function.However, in that case the actions you perform inside the function would have to be same in either case. In this case the compiler will generate source code of the functions for your both the types.

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

Sidebar

Related Questions

Update: After playing around with this for a few hours, went with a multi-query
After some time I wanted to update my git repo, and then something went
Update: Based on the answers I initially went the route of using IsInstanceOf() which
Our proxy went down and I tried to update dependencies with Maven while it
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update : Looks like the query does not throw any timeout. The connection is
Update: Now that it's 2016 I'd use PowerShell for this unless there's a really
I've just upgraded Magento from 1.4 to 1.5. The update went (almost) smoothly and
UPDATE 14 June 2011 A quick update... Most respondents have focused on the dodgy
I am not sure what went wrong but i could not able top use

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.