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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:21:56+00:00 2026-06-13T08:21:56+00:00

Consider the following code: #include<iostream> #include<vector> using namespace std; class Foo { public: template<

  • 0

Consider the following code:

#include<iostream>
#include<vector>
using namespace std;
class Foo {
public:
  template< typename T> 
  operator vector< T >() const {
    return vector< T >();
  }

  template< typename T> 
  operator T() const {
    return T();
  }
};

int main () {
  Foo b;
  vector< int >  q = b;
  q = b;
}

Compiling this with Clang or g++ using either of the two commands:

g++ test.cpp
clang++ test.cpp

Enabling C++11 features, however, it fails:

g++ --std=c++0x test.cpp
clang++ --std=c++11 test.cpp

The error message reads as follows:

test.cpp:20:5: error: use of overloaded operator '=' is ambiguous (with operand types 'vector<int>' and 'Foo')
  q = b;
  ~ ^ ~
/usr/include/c++/4.6/bits/stl_vector.h:373:7: note: candidate function
      operator=(vector&& __x)
      ^
/usr/include/c++/4.6/bits/stl_vector.h:362:7: note: candidate function
      operator=(const vector& __x);
      ^
/usr/include/c++/4.6/bits/stl_vector.h:394:7: note: candidate function
      operator=(initializer_list<value_type> __l)
      ^
1 error generated.

It is unclear to me why it works without C++11, while it fails with. Moveover, note that the line

vector< int >  q = b; // In the main function, line 19

in the main function does not cause an error. Can anyone explain why it does not work, and what one can do to make it work with C++11?

  • 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-13T08:21:57+00:00Added an answer on June 13, 2026 at 8:21 am

    There’s no compiler bug here. Your code is broken in C++11, because C++11 added more converting constructors and more overloads for the assignment operator.

    This is the risk you run when you make a type that converts to absolutely anything (using templated conversion). Foo is just as happy to convert itself to an initializer_list<int> as a vector<int>.

    The reason that

    vector<int> q = b;
    

    works in Clang 3.1, while

    vector<int> q(b);
    

    fails, is that the first is copy-initialization, which requires an implicit conversion to vector<int> followed by a copy-constructor call, while the second is direct-initialization which performs an explicit conversion. The set of candidates for implicit conversion is smaller, because constructors marked explicit are removed, resolving the ambiguity.

    The difference between Clang 3.0 and 3.1 is likely a library compliance fix, which marked additional constructors as explicit, not a change to compiler behavior.

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

Sidebar

Related Questions

Consider the following sample code: #include <iostream> using namespace std; class base { public:
Consider the following code: #include <iostream> using namespace std; class B{ public: B(){} };
Consider the following code: #include <iostream> #include <cstdio> #include <cstring> using namespace std; template<class
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
Consider the following code: #include <iostream> using namespace std; class Test { static int
Consider the following code: #include<iostream> using namespace std; class sample { int a; int
Consider the following sample code below: #include <iostream> using namespace std; class base {
please consider following code #include <iostream> using namespace std; class Digit { private: int
Consider the following program: #include <iostream> #include <algorithm> using namespace std; template<class T> struct

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.