c++ - 模板类构造函数重载决议

标签 c++ templates constructor overload-resolution constructor-overloading

我对类模板的 ctor 重载解析有一个简单的问题:

#include <iostream>
#include <string>

using namespace std;
enum EnumTypeVal { READ, WRITE };

template <class T>
class TemplateClassTest {
public:
    TemplateClassTest(const string & stringVal, T typeVal, EnumTypeVal e = READ,
                      const string & defaultStringVal = "default");
    TemplateClassTest(const string & stringVal, const char * charVal);
    TemplateClassTest(const string & stringVal, EnumTypeVal e = READ,
                      const string & defaultStringVal = "default");
private:
T type;
};

template <class T>
TemplateClassTest<T>::TemplateClassTest(const string & stringVal, T typeVal,
                                        EnumTypeVal e,
                                        const string & defaultStringVal)
{
    type = typeVal;
    cout << "In TemplateClassTest(const string &, T, EnumTypeVal, "
            "const string &)" << endl;
}

template <class T>
TemplateClassTest<T>::TemplateClassTest(const string & stringVal,
                                        const char * charVal)
{
    cout << "In TemplateClassTest(const string &, const char *)" << endl;
}

template <class T>
TemplateClassTest<T>::TemplateClassTest(const string & stringVal, EnumTypeVal e,
                                        const string & defaultStringVal)
{
    cout << "In TemplateClassTest(const string &, EnumTypeVal, const string &)"
         << endl;
}

typedef TemplateClassTest<long long unsigned int> u32Type;
typedef TemplateClassTest<bool> boolType;

int main()
{
    u32Type l("test", "0"); //matches ctor 2
    u32Type v("test", 0); // ambiguity between ctor 1 and 2
    boolType b("test", "true"); //matches ctor 2
    return 0;
}

第二次调用因抛出错误而无法编译:

Call of overloaded 'TemplateClassTest(const char [5], int) is ambiguous.

为什么 int 匹配到 const char *?这种情况可以通过将ctor 2中的const char *改为const string &来解决。但是这样做,boolType b("test", "true") 现在匹配到 ctor 1 而不是 ctor 2。

我的要求是:

  • u32Type v("test", 0) 应该匹配 ctor 1
  • boolType b("test", "true") 应该匹配构造函数 2。

限制是:

  • ctor 1 和 3 签名不能更改
  • main() 中的用户代码调用不能更改。

非常感谢任何帮助..谢谢!

最佳答案

这是不明确的,因为 0 是一个空指针常量。空指针常量可以隐式转换为任何指针类型 -> 空指针转换。因此,ctor 2 是可行的:TemplateClassTest(const string&, const char*)

作为整数文字,0int 类型。构造函数 1,TemplateClassTest(const string&, T, EnumTypeVal = READ, const string & = "default"),因此需要从 int 转换为 unsigned long long -> 积分转换

空指针转换整数转换这两个转换具有相同的等级(转换),因此存在歧义。


[conv.ptr]/1

A null pointer constant is an integral constant expression prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion.


一个符合您的约束条件的可能但丑陋的修复方法是将第二个构造函数更改为:

template<class U,
         class V = typename std::enable_if<std::is_same<U, const char*>{}>::type>
TemplateClassTest(const string & stringVal, U charVal);

即贪心构造函数模板,受 SFINAE 限制,仅接受 const char* 作为第二个参数。这严重限制了在尝试匹配此 ctor 时应用于第二个参数的隐式转换。

线外定义变为:

template <class T>
template<class U, class V>
TemplateClassTest<T>::TemplateClassTest(const string & stringVal, U charVal)
{
    cout << "In TemplateClassTest(const string &, const char *)" << endl;
}

关于c++ - 模板类构造函数重载决议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19796413/

相关文章:

c++ - 预编译头 C++ 无法重命名 pch.h

c++ - 是否可以在 C++ 中实现事件?

c++ - 编译时枚举字符串整数对

c# - 使用 Enumerable.Empty<T>() 而不是 new List<T>() 来初始化 IEnumerable<T> 是否更好?

c# - 为继承的只读字段赋值?

constructor - kotlin,如何简化将参数传递给基类构造函数?

c++ - 静态链接 wxWidgets 时的引用错误

c++ - 带有指针参数的函数模板重载决策

c++ - std::to_string(double) 的精度

c++ - 找不到 'std::transform..."的匹配项