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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:09:56+00:00 2026-05-28T02:09:56+00:00

First is the code #include <stdio.h> typedef wchar_t* BSTR; wchar_t hello[] = LHello; class

  • 0

First is the code

#include <stdio.h>

typedef wchar_t* BSTR;

wchar_t hello[] = L"Hello";

class _bstr_t {
public:
    operator const wchar_t*() const throw() { return hello; }
    operator wchar_t*() const throw() { return hello; }
};

class container {
public:
    operator _bstr_t() { return _bstr_t(); }
};

int main()
{
    // This gives error (with gcc 4.5.2 at least):
    // test.cpp:20:27: error: cannot convert "container" to "wchar_t*" in initialization
    wchar_t *str = container();
    printf("%S\n", str);
    return 0;
}

The problem here is that container() can be casted to _bstr_t and then to wchar_t*, but, gcc does not.

The problem can be solved using manual cast:

wchar_t *str = (_bstr_t)container();

But what I need is to avoid manual cast, I would like gcc to figure this out automatically.

Why I need this is because the returned container type objects will be used in calls like

void Func(wchar_t* str);
Func(myObject->Container);

where I don’t want to do manual casting.

I verified Visual Studio and it does not seem to support such scenario, too. Too bad, but I would be glad if someone can provide a workaround, even if for this specific case.

UPDATE: for those who suggests operator wchar_t* on the container, that was the problem in first place. This will either leak or crash when destroyed before Func() has a chance to accept the pointer.

  • 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-28T02:09:57+00:00Added an answer on May 28, 2026 at 2:09 am

    When doing implicit conversions, there’s at most one user-defined conversion that can happen. MSVC behaviour is not standard compliant in this matter.

    C++11 (12.3 Conversions):

    At most one user-defined conversion (constructor or conversion function) is implicitly applied to a single value.

    For the implicit conversion to work, container has to convert directly to wchar_t*.

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

Sidebar

Related Questions

This code #include <stdio.h> #include <stdlib.h> #include <time.h> int main () { printf (First
Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct
I often refactor code first by creating an inner class inside the class I'm
I have a .py file with lots of classes: class First(Second): #code class Third(Fourth):
I have these headers in a c code #include <stdio.h> #include <unistd.h> Everything compiled
I have a very simple c code: #include<stdio.h> int main() { enum boolean{true,false}; boolean
Been practicing with those system calls, but I stucked into this code: #include <stdio.h>
I came across this code: #include<stdio.h> void main() { int x; float t; scanf(%f,&t);
So lets take a look onto a littel bit modified example code: #include <stdio.h>
Consider the following code: #include <stdio.h> int main (void) { char str1[128], str2[128], str3[128];

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.