模板静态功能模板类类型扣除

template static function template class type deduction

本文关键字:类型 功能 静态      更新时间:2023-10-16

我想在单元模板类中发挥静态功能,可以扣除模板类的类型。问题是,从模板类调用静态函数需要明确的类型。我想出的唯一解决方法是模板函数,如果模板成员函数。

这是一个示例。问题是foo4部分不起作用

template <class T>
class Foo
{
private:
    Foo() {}
    Foo(const Foo&) = delete;
    Foo& operator= (const Foo&) = delete;
public:
    static auto& Instance()
    {
        static Foo foo{};
        return foo;
    }
    template<class K> static
    auto& DeductInstance(const K&)
    {
        static Foo<K> foo{};
        return foo;
    }
};
template<class K>
auto& DeductInstance(const K&)
{
    return Foo<K>::Instance();
}
void main()
{
    auto& foo1 = Foo<int>::Instance(); //OK
    auto& foo2 = Foo<int>::Instance(); //OK (return same example as foo1)
    auto& foo3 = DeductInstance(123); //OK
    auto& foo4 = Foo::DeductInstance(123); //NOT WORKING
}

使用注入的类名称在理论上是可能的。这将使Foo::解析为特定的,无关的Foo<x>::。这是一个例子:

struct BaseFoo {
    template<class K> static
    auto& DeductInstance(const K&);
};
template <class T>
struct Foo  {
    static auto& Instance() {
        static Foo foo{};
        return foo;
    }
};
template<>
struct Foo<void> : private BaseFoo {
    using BaseFoo::DeductInstance;
};
template<typename K>
auto& BaseFoo::DeductInstance(const K&)
{
    return Foo<K>::Instance();
}
using MakeFooAvailable = Foo<void>;
struct Dummy : MakeFooAvailable {
    auto& bar() {
        // Syntax working
        return Foo::DeductInstance(234);
    }
};

Dummy类中,基本类Foo的注入类名称,将其解析为Foo<void>。然后,Foo<void>正在使BaseFoo::DeductInstance在其范围中可分辨出来。


我建议不要使用此解决方案,因为它是一个聪明的解决方案。聪明通常意味着令人惊讶。程序员不希望将Foo视为非模板。我认为最好的解决方案是:

auto& foo1 = Foo<int>::Instance(); //OK

越简单,越好。

是2021,我们有CTAD,但我认为这没有帮助。我认为这与您(和我!)想要的东西非常接近:

// A helper struct to provide Foo with a default template 
// that isn't useful for instantiation:
struct DoNotInstantiate { DoNotInstantiate() = delete; };
template <class T = DoNotInstantiate>
class Foo
{
private:
    Foo() {
        static_assert(!std::is_same_v<T, DoNotInstantiate>, "You can't actually instantiate it with the default, it's just to make the static function work."); // Optional
    }
    // ...
public:
    // ...
    template<class K> static
    auto& DeductInstance(const K&)
    {
        static Foo<K> foo{};
        return foo;
    }
};

因此您可以致电Foo<>::DeduceInstance(bar);。您仍然必须编写<>,但否则,我认为这是完美的。

如果您不想要它并且确实希望能够在模板上调用静态,仅命名模板名称,则是:

template <template<typename ...> class FooType, typename T>
auto DeduceInstance(const T& x) {
    return FooType<T>::DeduceInstance(x);
}

可以让您调用DeduceInstance<Foo>(x),其中Foo只是一个模板。但这感觉很圆。我更喜欢Foo<>::DeduceInstance(x);

如果CTAD让您写

,那将是很好的
template <class T>
Foo::DeductInstance(const T&) -> T&;

或可能是

template <class T>
Foo::DeductInstance(const T&) -> Foo<T>::DeduceInstance(const T&);

或类似的内容,基本上说,如果您使用没有类型的模板名称来调用静态,请使用参数列表来涂上呼叫者正在谈论的模板。

我必须承认我不完全理解您要做的事情。但是,您可以做以下两个:

struct foo {
    template <typename T> 
    static T deduce(const T& t) { 
        return {};
    }
};
template <typename T> 
T deduce_free(const T& t) {
    return {};
}
int main() {
    auto x = foo::deduce(1);
    auto y = deduce_free(1);
}

但是,在知道T是什么之前,您无法在bar<T>的某些实例上调用方法(是否静态)。

c 17的情况有所改变,尽管我知道这适用于构造函数,但,因此您仍然需要foo<int>,然后才能调用foo<int>::deduce() 我对那说话不错的东西太不经验了;)。