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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:27:15+00:00 2026-05-17T06:27:15+00:00

I am trying to create a value template class, where additional properties can be

  • 0

I am trying to create a “value” template class, where additional properties can be assign to it easily.
Properties are stored in std::map<std::string, std::string>, and operator[] has been overloaded to provide quick access to them.

#if ! defined __VALUE_H__
#define __VALUE_H__

#include <string>
#include <map>
namespace Algorithm
{
    template<typename TValueType> 
    class CValue
    {
        public:
            CValue(const TValueType& Value) : 
            m_Value(Value)
            {
            }

            ~CValue()
            {
            }

            CValue(const CValue& Source) :
            m_Value(Source.m_Value),
            m_mssProperties(Source.m_mssProperties)
            {
            }

            CValue& operator=(const CValue& Source) 
            {
                if (this != &Source)
                {
                    m_Value = Source.m_Value;
                    m_mssProperties = Source.m_mssProperties;
                }
                return *this;
            }

            CValue& operator=(const TValueType& Source)
            {
                m_Value = Source;
                return *this;
            }

            std::string& operator[](const std::string& sPropertyName)
            {
                return m_mssProperties[sPropertyName];
            }

            std::string operator[](const std::string& sPropertyName) const
            {
                std::map<std::string, std::string>::const_iterator iter;
                iter = m_mssProperties.find(sPropertyName);
                return ((iter!=m_mssProperties.end()) ? iter->second : "");
            }

            operator TValueType () const
            {
                return m_Value;
            }

        private:
            TValueType m_Value;
            std::map<std::string, std::string> m_mssProperties;
    };
};

#endif //__VALUE_H__

Why does when I invoke operator[] with string laterals, MSVC2008 complains on overloads ambiguity.

#include <string>
#include "Value.h"

int main()
{
    std::string valName = "XX";
    std::string valProp = "YY";
    Algorithm::CValue<int>  obj(valName, 2);
    obj[valProp] = "ZZ";
    obj["AA"] = "BB";       // compiler error
    return 0;
}

Compiler error:

1>------ Build started: Project: TemplateHell, Configuration: Debug Win32 ------
1>Compiling...
1>TemplateHell.cpp
1>c:\documents and settings\yeen-fei.lim\my documents\visual studio 2008\projects\templatehell\templatehell.cpp(13) : error C2666: 'Algorithm::CValue<TValueType>::operator []' : 3 overloads have similar conversions
1>        with
1>        [
1>            TValueType=int
1>        ]
1>        c:\documents and settings\yeen-fei.lim\my documents\visual studio 2008\projects\templatehell\value.h(146): could be 'std::string Algorithm::CValue<TValueType>::operator [](const std::string &) const'
1>        with
1>        [
1>            TValueType=int
1>        ]
1>        c:\documents and settings\yeen-fei.lim\my documents\visual studio 2008\projects\templatehell\value.h(134): or       'std::string &Algorithm::CValue<TValueType>::operator [](const std::string &)'
1>        with
1>        [
1>            TValueType=int
1>        ]
1>        or       'built-in C++ operator[(int, const char [3])'
1>        while trying to match the argument list '(Algorithm::CValue<TValueType>, const char [3])'
1>        with
1>        [
1>            TValueType=int
1>        ]
1>Build log was saved at "file://c:\Documents and Settings\yeen-fei.lim\My Documents\Visual Studio 2008\Projects\TemplateHell\Debug\BuildLog.htm"
1>TemplateHell - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Edit:

When i remove implicit conversion-operator from the code, it will compile without error. But it makes no sense for me because it is never used so far.

  • 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-17T06:27:16+00:00Added an answer on May 17, 2026 at 6:27 am

    obj["AA"]… have you seen a similar construct before? There is an old C obfuscation trick where you take the array and the index and reverse them in the expression as a[b] is the same as *(a + b) which is the same as b[a]. In the cast overload in this case, you are returning a integer. The other element of the call is a character array. This confuses the compiler as it doesn’t know if this is you trying to get element obj of "AA", or if you want the overloaded operator. You can see this in the output of the following program:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    template <typename T>
    class Val {
        T const ret;
    public:
        Val(T r) : ret(r) {}
    
        operator T () const {
            return ret;
        }
    };
    
    int main()
    {
        Val<int> v(3);
        cout << v["abcd"] << endl;
        return 0;
    }
    

    d

    Consider the following:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    template <typename T>
    class Val {
        T const ret;
    public:
        Val(T r) : ret(r) {}
    
        operator T () const {
            return ret;
        }
        string operator[](string const &) const {
            return string("Hi!");
        }
    };
    
    int main()
    {
        Val<int> v(3);
        cout << v["Hello!"] << endl;
        return 0;
    }
    

    This gives a similar error. Change Val<int> to Val<double> and the problem magically goes away (you can’t index an array with a double; no confusion).

    The solution in this case is to create another overload: string operator[](char const *) const. This gets rid of the confusion.

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

Sidebar

Related Questions

I'm trying to create a template function which will iterate through the map's specified
I am trying to create a lightweight template class having a static member initialized
I'm trying to write a conversion operator function template in a class and running
I am trying to create a checkbox limit based on a value change example:
I am trying to create a function that updates another value, and initially (after
I am trying to create a dynamic hyperlink that depends on a value passed
I'm trying to create text in html, that once clicked, the the value of
I am trying to create a table with values that are in a String
I'm trying to create am immutable type (class) in C++, I made it so
I'm currently trying to create a ControlTemplate for the Button class in WPF, replacing

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.