有没有办法使用概念来禁用会产生对void的引用的成员函数?

我希望可以编写这样的类

template <class T>
struct A {
    T& operator*() 
        requires (!std::is_void_v<T>)
    {
        return *ptr;
    }
    T* ptr;
};

但是如果我写

A<void> a;

我收到编译器错误

prog.cc: In instantiation of 'struct A<void>':
prog.cc:16:13:   required from here
prog.cc:5:8: error: forming reference to void
    5 |     T& operator*()
      |        ^~~~~~~~

即使 requires 子句禁用了该功能。

有没有办法编写类,以便编译器可以接受禁用的方法?

(我知道我可以将 A 部分专用于 void ,但这不太方便)。

回答

不,不幸的是这是不可能的。我希望你写的东西真的有用,如果有的话这将是正确的写法,但你就是做不到。

您的选择是:

template <class T>
struct A {
    // #1: a type trait that handles void for you
    std::add_lvalue_reference_t<T> operator*() requires (!std::is_void_v<T>);

    // #2: make it a template (with only one valid instantiation)
    template <std::same_as<T> U=T>
        requires (!std::is_void_v<U>)
    U& operator*();

    // #3: use auto, even though we know the type and it's easy to spell
    auto& operator*() requires (!std::is_void_v<T>);
};

我不喜欢他们三个。你的旅费可能会改变。


以上是有没有办法使用概念来禁用会产生对void的引用的成员函数?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>