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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:26:54+00:00 2026-06-09T02:26:54+00:00

I am a bit in stuck and need a help from C++ template guru.

  • 0

I am a bit in stuck and need a help from C++ template guru. There is a template struct:

template<typename T, typename ID>
struct TypeMapping
{
    T Type;
    char* Name;
    ID Id;
};

and a few template functions like this:

template<typename T, typename ID>
bool TryGetTypeByNameImp(const TypeMapping<T, ID> map[], size_t mapSize,
    const char* name, T& type)
{
    for (size_t i = 0; i < mapSize; i++)
    {
        if (strcmp(map[i].Name, name) == 0)
        {
            type = map[i].Type;
            return true;
        }
    }

    return false;
}

Map (the first parameter) is defined as (there are a few similar maps)

namespace audio
{ 
    const TypeMapping<Type, AMF_CODEC_ID> Map[] = 
    {
        {AAC, "aac", AMF_CODEC_AAC},
        {MP3, "mp3", AMF_CODEC_MP3},
        {PCM, "pcm", AMF_CODEC_PCM_MULAW}
    };

    const size_t MapSize =  sizeof(Map)/sizeof(Map[0]);
}

Map is passed to a function as an argument and I am looking for how to pass it as template parameter so I can use functions like in this sample:

 audio::Type type;
 bool r = TryGetTypeByNameImp<audio::Map>("aac", type);

The only solution I found it is to define a struct which holds static Map and MapSize and use the struct as template parameter but I do not like this solution and I am looking for another one. Does anybody know how to do this?

  • 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-09T02:26:56+00:00Added an answer on June 9, 2026 at 2:26 am
    bool r = TryGetTypeByNameImp<audio::Map>("aac", type);
    

    This is trying to use audio::Map as a type – but it isn’t, it’s a variable. Just pass it to the function as a normal argument:

    bool r = TryGetTypeByNameImp(audio::Map, "aac", type);
    

    That said, I have three remarks about your code:

    • Be aware that declaring a function argument as an array (x[]) does in reality declare it as a pointer. Your code uses this correctly, but using the array syntax is misleading. Use a pointer instead.
    • This code is slightly too C-heavy for my taste. While I agree that using raw C-strings is appropriate here, your usage of char* is illegal in C++11, and deprecated in C++03 (since you are pointing to string literals). Use char const*. Furthermore, I’d suggest using a std::string argument in the function, and using the comparison operator == instead of strcmp.
    • You are using an out-parameter, type. I abhor this technique. If you want to return a value, use the return type. Since you also return a success value, use a pair as the return type, unless there’s a very compelling reason not to:

      template<typename T, typename ID>
      std::pair<bool, T> TryGetTypeByNameImp(
          const TypeMapping<T, ID> map[], size_t mapSize,
          const char* name)
      {
          for (size_t i = 0; i < mapSize; i++)
              if (strcmp(map[i].Name, name) == 0)
                  return std::make_pair(true, map[i].Type);
      
          return std::make_pair(false, T());
      }
      

    Ah, and I’d also consider using a std::vector or std::array here instead of a C array. Then you don’t need to manually shlep the array size around through all the functions which use the array.

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

Sidebar

Related Questions

I'm a bit stuck repairing a faulty table (on Hbase 0.92.1-cdh4.0.0, Hadoop 2.0.0-cdh4.0.0) There
I am a bit stuck on one problem for a few hours now. Let`s
Really hope someone can help me as I'm a bit stuck :S I have
I am a bit stuck here and can't think further. public struct CandidateDetail {
i need a little bit of help with some jquery thing that i'm sure
Am a bit stuck with a wordpress loop, am wondering if anyone can help.
noob here and need help from the pros... i am doing a voice record
I am a little bit stuck up. First, I'll post the screenshot and then
I am a little bit stuck: I would like to change the max option
I'm a bit stuck with my code: def setVenueImage(img): img = images.Image(img.read()) x, y

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.