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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:09:07+00:00 2026-05-26T21:09:07+00:00

The original problem was how to work with std::map<std::wstring, std::wstring> > in a secure

  • 0

The original problem was how to work with std::map<std::wstring, std::wstring> > in a secure way because equal types of the key and the value are extremely error-prone. So I decided to create a simple wrapper for the value:

    struct ComponentName
    {
      std::wstring name;

      // I want to prohibit any implicit string-ComponentName conversions!!!
      explicit ComponentName(const std::wstring& _name) : name(_name)
      {
      }

      bool operator<(const ComponentName& item_to_compare) const
      {
        return name < item_to_compare.name;
      }
    };

    typedef std::map<std::wstring, ComponentName> component_names_map;

But the following code works well!

component_names_map component_names;
// Are you sure that ComponentName's constructor cannot be called implicitly? ;)
component_names_map::value_type a_pair = std::make_pair(L"Foo", L"Bar");

It works because the std::pair<std::wstring, ComponentName> copy constructor explicitly uses the string contructor of the ComponentName to assign the std::pair<std::wstring, std::wstring> instance. It’s an absolutely legal operation. However it looks as an ‘implicit’ call of the ComponentName constructor.

So I know the reason of the problem, but how can I avoid this ‘implicit’ wstring-ComponentName conversion?
The simplest way is to not declare the string constructor, but it makes ComponentName initialization inconvenient.

  • 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-26T21:09:07+00:00Added an answer on May 26, 2026 at 9:09 pm

    I think you can legally do this by adding a partial specialisation of std::pair for your type:

    namespace std {
        template <typename T>
        struct pair<T,ComponentName> {
           typedef T first_type;
           typedef ComponentName second_type;
    
           T first;
           ComponentName second;
           // The rest of the pair members:
           // ....
           // Any trick you like to make it fail with assignment/construction from 
           // pair<std::wstring, std::wstring>
        };
    }
    

    Justification:

    § 17.6.4.2.1 sets out the basic rules for specialisations in the std namespace:

    “A program may add a template specialization for any standard library
    template to namespace std only if the declaration depends on a
    user-defined type and the specialization meets the standard library
    requirements for the original template and is not explicitly
    prohibited”

    I can’t see any explicit prohibition that would rule this particular case out, provided you filled out the rest of the class, within the bounds of § 20.3 .


    Alternative, possibly legal approach:

    Specialize std::is_constructible<ComponentName, std::wstring> such that value is false. This is listed as a requirement of both the assignment operator and the copy constructor for std::pairs of different types. I can’t see any prohibitions from a quick scan of this either, but I can’t find anything saying that implementations are required to check the requirements.

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

Sidebar

Related Questions

Original Problem: I have 3 boxes each containing 200 coins, given that there is
Original Problem In building our projects, I want the mercurial id of each repository
Original problem: What is the right column format for a unix timestamp? The net
Solution to original problem (below) may have been discovered. I commented out <identity> ...
This is a simplified version of the original problem. I have a class called
ORIGINAL TEXT REMOVED OK, so I found the original problem thanks to a helpful
The code below is (hopefully) a minimized testcase of my original problem. I'm trying
I've editing this original question as I think I've narrowed down the problem... I
Here's a perfect example of the problem: Classifier gem breaks Rails . ** Original
So the original problem is that we run an industry standard java based web

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.