An explicit C ++ constructor called in an implicit situation

I compiled the code below using g ++ 6.3.0, with the option -std = C ++ 14.

#include <utility>
#include <iostream>
struct A{
    int x;
    A(const A&)=default;
    A(int x):x(x){}
};

struct B{
  A a;
  template<class... Args>
  B(Args&&... args):a(std::forward<Args>(args)...){
    std::cout<<"1!"<<std::endl;
  }

  explicit B(const A& a):a(a){std::cout<<"2!"<<std::endl;}
};
struct C:B{
  using B::B;
};
int main(){
    A a{2};
    const A& aref=a;
    C c=aref; //Implicit conversion
}

I expected him to print "1!" since the conversion is implicit, but it outputs "2!". If I comment out the template constructor, it will not compile. Is this the correct behavior, or is it some kind of g ++ bug?

+4
source share

Source: https://habr.com/ru/post/1685769/


All Articles