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

The Archive Base Latest Questions

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

I have a C++ program with many thousands of string literals in the code

  • 0

I have a C++ program with many thousands of string literals in the code which need to be translated, for example:

statusBar->Print( "My Message" );

I wrapped the string literals with a function which looks up the value in a dictionary and returns the translated version:

statusBar->Print( Translated( "My Message" ) );

The problem is that after profiling I’ve discovered that doing this look up all over the code is a performance problem. What I’d like to do is change lines like that to:

static const char * translatedMessage5 = Translated( "My Message" );
statusBar->Print( translatedMessage5 );

But due to the many thousands of instances of this in the code, it’s going to be error prone (and a bit of a maintenance nightmare). I was hoping that I could turn Translated into a macro which declared the static variable in-line. This obviously doesn’t work. Anyone have a better idea?

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

    I/O time needed to print your message should be several orders of magnitude more than any dictionary lookup time. If this is not the case, you are doing something wrong.

    Tried and tested software is available that does what you need. I suggest you either study GNU Gettext, which is used by every other FOSS project out there, or just use it in your program instead of a homebrew solution.

    EDIT: With C++0x it is possible to do what you want, but still consider using GNU Gettext as your real l10n engine. Here’s some proof-of-concept little code:

    #include <iostream>
    
    const char* realTranslate(const char* txt)
    {
      std::cout << "*** translated " << txt << std::endl;
      return txt; // use a real translation here such as gnu gettext
    }
    
    #define Translate(txt) \
           (([]()->const char* \
             {static const char* out = realTranslate(txt); return out;})())
    
    int main ()
    {
      for (int i = 0; i < 10; ++i)
        {
          std::cout << Translate("This is a message") << std::endl;
          std::cout << Translate("This is a message") << std::endl;
          std::cout << Translate("This is another message") << std::endl;
        }
    }
    

    I’m not sure what the real C++ standard is going to specify, but under gcc-4.6 the realTranslate() function is called 3 times.

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

Sidebar

Related Questions

I have a program which needs to turn many large one-dimensional numpy arrays of
I have a program in which each thread reads in files many lines at
I have a program where I must print many STL vectors on the screen
Ok, so I have this program with many (~300) threads, each of which communicates
I have written a java program which needs to process thousands of text files
I have a program that has many available options. For example a configuration option
I have a Java program that runs many small simulations. It runs a genetic
I have a 'server' program that updates many linked lists in shared memory in
Solution for writing a program for keep secret photos. I have many secret photos
I have a Python program that spawns many threads, runs 4 at a time,

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.