C++ 编译器在使用 Bool 包装器时报告不明确的函数调用 Class

C++ Compiler Reports Ambiguous Function Call When Using Bool Wrapper Class

我有一个delima。我正在为本机类型使用包装器 类,但是,当使用包装器类型作为函数参数时,char pointerbool[= 的隐式转换22=] 不断导致编译器发出不明确的函数调用错误:

class VBool
{
   public:
      VBool(bool b):value(b){}

      template<class T>
      VBool(T)=delete;

   private:
      bool value;
};

class VString
{
   public:
      VString(const char* str):value(str){}

   private:
      std::string value;
};

void processVType(VBool vb){}
void processVType(VString vs){}

int main()
{
   processVType(""); // rejected as ambiguous by compiler.

   return 0;
}

现在编译器允许: VBool b = 真; 并正确拒绝: VBool b = "string";

但是如何让编译器正确识别要调用的预期函数版本?

注意:我使用的是启用了 C++17 语言标准的 VCC 编译器。

添加另一个具有适当参数的函数。

inline void processVType(const char* vs) { processVType(VString(vs)); }

出于好奇,你为什么要尝试实现这样的东西?