使用std :: enable_if声明带有可变参数模板args的括号运算符

时间:2012-11-27 23:15:57

标签: c++ templates gcc c++11 sfinae

我有以下smart_ptr课程。

template <typename T>
class smart_ptr
{
public:
    // ... removed other member functions for simplicity
    T* get() { return ptr; }

    template <typename... Args>
    decltype(T::template operator ()(Args()...)) operator ()(Args... args) const
    {
        return (*get()).operator ()(args...);
    }

private:
    T* ptr;
};

但是,当我在没有smart_ptr的情况下对类型T使用operator ()类时,它无法编译(并且正确地如此)。

我想要的是仅在T有std::enable_if时才使用operator ()来启用成员函数。当我开始这样做时,它变得非常模糊和复杂,因为我正在考虑使用SFINAE来检测T是否有operator ()然后将它与std::enable_if结合起来得到我想要的东西。但是,我迷失了创建能够检测可变模板化operator () 的SFINAE(即 NOT 重复Is it possible to write a template to check for a function's existence?)。

有人可以帮忙吗?或者给出另一个(也许更简单)的解决方案?

P.S。它必须适用于GCC 4.5.3。

编辑:为了完整起见,我已经用GCC 4.5.3的工作解决方案回答了我自己的问题。

2 个答案:

答案 0 :(得分:2)

编辑:与原始答案完全不同,后者没有给出真正的解决方案。

此代码在使用gcc 4.5.1的ideone上编译。这是我能做的最好的事情。

#include <array>
#include <iostream>

using namespace std;

template<typename T>
struct has_property
{
    struct UnmentionableType { UnmentionableType() {} };

    //test if type has operator() (...)
    template<typename U, typename A1, typename A2, typename A3, typename A4, typename A5>
    static auto ftest(U* t, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) -> decltype((*t)(a1, a2, a3, a4, a5), char(0));
    static std::array<char, 2> ftest(...);

public:
    static const bool value = sizeof(ftest((T*)0, UnmentionableType(), UnmentionableType(), UnmentionableType(), UnmentionableType(), UnmentionableType())) == 1;
};

class c1
{
public:
    int operator() () {  return 0; }
};

class c2
{
public:
    void operator() (int) { }
};

class c3
{
public:
    template<typename... Args>
    void operator() (Args... a) { }
};

class c4
{
public:
    template<typename T>
    void operator() (T t) { }
};

int main()
{
    cout<<boolalpha<<has_property<c1>::value<<endl;
    cout<<boolalpha<<has_property<c2>::value<<endl;
    cout<<boolalpha<<has_property<c3>::value<<endl;
    cout<<boolalpha<<has_property<c4>::value<<endl;
}

输出:

false
false
true
false

注意:

1)根据要求,它仅在可变参数化运算符()上触发(好吧,差不多)

2)这不是一个“完美”的解决方案,但它应该足够实用。

不幸的是gcc 4.5.1无法扩展参数包;我怀疑这可能适合你:

template<typename U, typename... A>
static auto ftest(U* t, A... a) -> decltype((*t)(a...), char(0));

我仍然没有看到实际测试函数真正可变参数的方法,而不是只传递5个(或更多)随机类型参数。

用法with_enable if - 除了部分类专业化之外我没有看到任何其他解决方案:

//non-specialized
template<typename T, typename E=void>
class container
{
    T* ptr;

public:
    T* get() { return ptr; }
};

//specialization for class with appropriate operator()
template<typename T>
class container<T, typename std::enable_if<has_property<T>::value, void>::type>
{
    T* ptr;

public:
    T* get() { return ptr; }

    template <typename... Args>
    decltype(T::template operator ()(Args()...)) operator ()(Args... args) const
    {
        return (*get()).operator ()(args...);
    }
};

答案 1 :(得分:1)

最后,我找到了GCC 4.5.3的解决方法!

template <typename T>
class smart_ptr
{
public:
    // ... removed other member functions for simplicity
    T* get() const { return ptr; }

private:
    template <typename... Args>
    struct opparen_constret
    {
        typedef decltype(std::declval<T const>()(std::declval<Args>()...)) type;
    };

    template <typename... Args>
    struct opparen_ret
    {
        typedef decltype(std::declval<T>()(std::declval<Args>()...)) type;
    };

public:
    template <typename... Args>
    typename opparen_constret<Args...>::type operator ()(Args... args) const
    {
        return (*get())(args...);
    }

    template <typename... Args>
    typename opparen_ret<Args...>::type operator ()(Args... args)
    {
        return (*get())(args...);
    }

private:
    T* ptr;
};