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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:01:32+00:00 2026-06-04T16:01:32+00:00

I am testing out a way to mimic C# properties and created the following

  • 0

I am testing out a way to mimic C# properties and created the following property class:

struct BY_REF
{
    template <class T>
    struct TT_s
    {
        typedef T &TT_t;
    };
};
struct BY_VAL
{
    template <class T>
    struct TT_s
    {
        typedef T TT_t;
    };
};

template <class T, class P=BY_REF>
class property
{
private:
    typedef typename P::template TT_s<T>::TT_t TT;
    T &value;
    property();
    property(const property &);
    property &operator=(const property &);
public:
    explicit property(T &v) : value(v) {}
    operator const TT() const
    {
        return value;
    }
    TT operator=(const TT i)
    {
        return value = i;
    }
};

I tested this class with the following code:

int main()
{
    int i;
    std::string s;
    property<int, BY_VAL> I(i);
    property<std::string> S(s);
    //stringproperty S(s);
    I = 1337;
    char c[] = "I am ";
    S = std::string(c);
    cout << /*S <<*/ I << endl;
    return 0;
}

This gives me an unexpected compiler error, “no match for 'operator='…”, for the line S = std::string(c);. I commented out the printing of S because I the operator= problem seems to be simpler, and I hope its solution will solve the operator<< problem as well. To try to figure out what is going on, I instantiated the template manually as follows:

class stringproperty
{
private:
    std::string &value;
    stringproperty();
    stringproperty(const stringproperty &);
    stringproperty &operator=(const stringproperty &);
public:
    explicit stringproperty(std::string &v) : value(v) {}
    operator const std::string &() const
    {
        return value;
    }
    std::string &operator=(const std::string &i)
    {
        return value = i;
    }
};

My manual version works. Can anyone explain why the template version doesn’t? (I suspect it has something to do with the BY_REF and BY_VAL classes but then I do not know why it works for integers.)

  • 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-04T16:01:34+00:00Added an answer on June 4, 2026 at 4:01 pm

    Your manual version is erroneous, and the issue has nothing to do with templates.

    typedef int& IntRef;
    
    const int& == int const&
    
    const IntRef == IntRef const == int& const
    

    Notice the difference ? The issue is thus there: TT operator=(const TT i).

    The general guideline is that if you want to treat a typedef as a simple text replacement, then you need to start right now: Put the const after the type it qualifies.

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

Sidebar

Related Questions

I'm testing out crossroads.js and hasher.js together and the following code errors on the
I was trying to figure out a way to do the following var foo
Testing out someone elses code, I noticed a few JSP pages printing funky non-ASCII
In testing out our API, one of our testers found out that when they
i'm just testing out the csv component in python, and i am having some
I am having issues testing out the Scala Parser Combinator functionality for a simple
I was testing out the db2 max function to see if it picks NULL
I am testing out IF statements in MySQL for the first time and I
I have started to try out noSQL databases now and are currently testing out
I'm testing this out in LINQPad with the CRM plugin. What should my query

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.