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.
I think you can legally do this by adding a partial specialisation of
std::pairfor your type:Justification:
§ 17.6.4.2.1 sets out the basic rules for specialisations in the
stdnamespace: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 thatvalueis false. This is listed as a requirement of both the assignment operator and the copy constructor forstd::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.