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

  • Home
  • SEARCH
  • 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 8511641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:03:40+00:00 2026-06-11T04:03:40+00:00

The following code compiles in Visual Studio 2010 but fails to compile in the

  • 0

The following code compiles in Visual Studio 2010 but fails to compile in the Visual Studio 2012 RC.

#include <string>

// Windows stuffs
typedef __nullterminated const wchar_t *LPCWSTR;

class CTestObj {
public:
    CTestObj() {m_tmp = L"default";};

    operator LPCWSTR()  { return m_tmp.c_str(); }       // returns const wchar_t*
    operator std::wstring() const { return m_tmp; }     // returns std::wstring

protected:
    std::wstring m_tmp;
};


int _tmain(int argc, _TCHAR* argv[])
{
    CTestObj x;
    std::wstring strval = (std::wstring) x;

    return 0;
}

The error returned is:

error C2440: ‘type cast’ : cannot convert from 'CTestObj' to 'std::wstring'
No constructor could take the source type, or constructor overload resolution was ambiguous

I’ve already realized that commenting out either of the conversion operators fixes the compile problem. I just want to understand:

  1. What‘s going on under the hood to cause this
  2. Why this compiles in VS2010 and not in VS2012? Is it because of a C++11 change?
  • 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-11T04:03:42+00:00Added an answer on June 11, 2026 at 4:03 am

    If I’m understanding the logic under the hood, the operator overload is trying to copy the code and the object every time you cast. Therefore, you need to return it as a reference instead of attempting to return a new object based on the field. The line:

    operator std::wstring() const { return m_tmp; }
    

    should be:

    operator std::wstring&() { return m_tmp; }
    

    The following compiles and runs as expected.

    #include <string>
    
    // Windows stuffs
    typedef __nullterminated const wchar_t *LPCWSTR;
    
    class CTestObj {
    public:
        CTestObj() {m_tmp = L"default";};
    
        operator LPCWSTR()  { return m_tmp.c_str(); }       // returns const wchar_t*
        operator std::wstring&() { return m_tmp; }     // returns std::wstring
    
    protected:
        std::wstring m_tmp;
    };
    
    
    int main()
    {
        CTestObj x;
        std::wstring strval = (std::wstring) x;
        wprintf(L"%s\n", strval.c_str());
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've found that the following code doesn't compile in Visual Studio 2010 (but works
The following source code compiles correctly with Visual Studio 2010: namespace NS { class
the following code compiles with Visual Studio 2008 but not with g++ on Mac
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
The following code will not compile against the Async CTP in Visual Studio 2010:
I came across strange behavior in Visual Studio 2010 C++ compiler. Following code compiles
The following code compiles fine on majority of compilers (include Microsoft Visual Studio's -
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
Visual Studio 2008 I am using the following source code that compiles ok using
UPDATE: The following code only makes sense in C#4.0 (Visual Studio 2010) It seems

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.