模板推导与隐式用户定义的转换运算符

8 c++ templates implicit-conversion c++-concepts c++20

我试图实现一个涉及模板的用户定义类型转换的小例子.

#include <cassert>
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <type_traits>

template <typename T>
concept bool UIntegral = requires() {
    std::is_integral_v<T> && !std::is_signed_v<T>;
};

class Number
{
public:
    Number(uint32_t number): _number(number)
    {
        if (number == 1) {
            number = 0;
        }

        for (; number > 1; number /= 10);
        if (number == 0) {
            throw std::logic_error("scale must be a factor of 10");
        }
    }

    template <UIntegral T>
    operator T() const
    {
        return static_cast<T>(this->_number);
    }

private:
    uint32_t _number;
};

void changeScale(uint32_t& magnitude, Number scale)
{
    //magnitude *= scale.operator uint32_t();
    magnitude *= scale;
}

int main()
{
    uint32_t something = 5;
    changeScale(something, 100);
    std::cout << something << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下编译错误(来自GCC 7.3.0):

main.cpp:在函数'void changeScale(uint32_t&,Number)'中:

main.cpp:40:15:错误:'operator*='不匹配(操作数类型是'uint32_t {aka unsigned int}'和'Number')

幅度*=规模;

注意该行已注释掉 - 这一行有效:

//magnitude *= scale.operator uint32_t();
Run Code Online (Sandbox Code Playgroud)

为什么不能自动推导模板化转换运算符?在此先感谢您的帮助.

[编辑]

我遵循删除概念的建议来使用Clang并查看其错误消息.我得到以下(这是截断但足够):

main.cpp:34:15: error: use of overloaded operator '*=' is ambiguous (with operand types 'uint32_t'
  (aka 'unsigned int') and 'Number')
magnitude *= scale;
~~~~~~~~~ ^  ~~~~~
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, float)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, double)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, long double)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, __float128)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, int)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, long)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, long long)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, __int128)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, unsigned int)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, unsigned long)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, unsigned long long)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, unsigned __int128)
Run Code Online (Sandbox Code Playgroud)

因此,在开启概念的情况下,我假设转换数字的唯一方法是将其作为无符号整数类型 - 那么为什么编译器不能推断转换呢?

rus*_*tyx 1

概念requires表达式的工作方式与 SFINAE 类似,它只检查表达式是否有效,但不对其求值。

要使概念实际上限制 T无符号整型,请使用bool表达式:

template<typename T>
concept bool UIntegral = std::is_integral_v<T> && !std::is_signed_v<T>;
Run Code Online (Sandbox Code Playgroud)

但这能解决你的问题吗?不幸的是没有,请继续阅读...

为什么不能自动推导模板化转换运算符?

编写有缺陷的 C++ 代码肯定会遇到编译器错误:-) gcc 中有超过 1,000 个已确认的未解决错误。

是的,应该找到模板化转换运算符,并且"no match for 'operator*='"错误消息应该改为"ambiguous overload for 'operator*='"

因此,打开这些概念后,我假设转换 Number 的唯一方法是将其转换为无符号整数类型 - 那么为什么编译器不足以推断转换呢?

即使概念要求和编译器错误得到修复,歧义仍然存在,特别是这四个:

main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, unsigned int)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, unsigned long)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, unsigned long long)
main.cpp:34:15: note: built-in candidate operator*=(unsigned int &, unsigned __int128)
Run Code Online (Sandbox Code Playgroud)

这是因为每种可以想象的提升的内置类型都有很多int内置运算符,并且、longlong long__int128都是整型

因此,将转换模板化为内置类型通常不是一个好主意。

解决方案1.制作转换运算符模板explicit并显式请求转换

    magnitude *= static_cast<uint32_t>(scale);
    // or
    magnitude *= static_cast<decltype(magnitude)>(scale);
Run Code Online (Sandbox Code Playgroud)

解决方案 2.只需实现到 类型的非模板转换_number

struct Number
{
    using NumberType = uint32_t;
    operator NumberType () const
    {
        return this->_number;
    }
    NumberType _number;
};
Run Code Online (Sandbox Code Playgroud)