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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:30:06+00:00 2026-05-31T01:30:06+00:00

Is it possible to tell g++ to use the FOO& operator when constructing the

  • 0

Is it possible to tell g++ to use the FOO& operator when constructing the FOO object ?

struct FOO {

        FOO( FOO & foo ) { // non-const copy constructor
        }

        operator FOO&() {

                return *this;
        }

        FOO( int i ) {
        }
};


int main() {

        FOO a(FOO(5));
}

I currently get the following error:

In function int main():
  error: no matching function for call to FOO::FOO(FOO)
  note: candidates are: FOO::FOO(int)
  note:                 FOO::FOO(FOO&)

— edit —

Note that I try to setup an object that can exchange the ownership of a resource.
Calling FOO foo1(foo) make foo to lose the ownership of the resource, this mean that foo cannot be const.
Also note that I want to avoid smart-pointer mechanism.

  • 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-31T01:30:08+00:00Added an answer on May 31, 2026 at 1:30 am

    Your conversion operator will never be picked up.

    §12.3.2 [class.conv.fct] p1

    A conversion function is never used to convert a (possibly cv-qualified) object to the (possibly cv-qualified) same object type (or a reference to it), to a (possibly cv-qualified) base class of that type (or a reference to it), or to (possibly cv-qualified) void.

    The reason is that the conversions mentioned here (except to cv void) are already done by so-called standard conversions (qualification conversion (adding const or volatile) and identity conversion (binding an object to a reference)) and standard conversions are always preferred to user-defined conversions:

    §13.3.3.2 [over.ics.rank] p2

    a standard conversion sequence (13.3.3.1.1) is a better conversion sequence than a user-defined conversion sequence […]


    For your specific case, if you want to transfer ownership, do so in C++11 style, with a move constructor.

    #include <utility> // move
    
    struct foo{
      int resource;
      foo(int r)
        : resource(r) {}
      foo(foo&& other)
        : resource(other.resource)
      { other.resource = 0; }
    };
    
    int main(){
      foo f1(foo(5));
      //foo f2(f1); // error
      foo f3(std::move(f1)); // OK
    }
    

    Transferring ownership via non-const copy constructors is a very bad idea, as such a type can never be stored in standard containers, see the ugly std::auto_ptr (replaced by std::unique_ptr in C++11, which has proper move semantics).

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

Sidebar

Related Questions

Please tell me whether it is possible to use both SimpleUrlHandlerMapping and AnnotationMapping in
Could anyone tell me if it is possible to use the flex 4 framework
Is it possible to tell Eclipse to use some extra arguments for aapt (-0
I think explain() will tell any possible index it can use. How about just
Is it possible to tell if a workbook has been opened from an email
I understand that it is not possible to tell what the user is doing
As the title says on a website is it possible to tell if a
From within a finally block, is it possible to tell an exception has been
Please tell me if it is possible to do the following: create an instance
Somebody please tell me it is possible to recover Visual Studio source after VS

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.