c++ - 在 vs2015 的可变参数模板中找不到重载的成员函数

标签 c++ templates visual-studio-2015 using

我试图将其归结为基本要素。 我有一个可变参数模板类 Foo,其中包含一个用其类型索引的对象“列表”。我使用函数 bar<U>()提取该类型的元素。我使用可变模板和 std::enable_if 来解决这个问题,只定义 bar<U>()其中 T == U。 然后我使用“using”从基类中公开所有“bar”函数。

#include <type_traits>

template<typename... Ts>
class Foo
{
public:
    void bar() {}
};

template<typename T, typename... Ts>
class Foo<T, Ts...> : public Foo<Ts...>
{
public:
    using Foo<Ts...>::bar;

    template<typename U>
    typename std::enable_if<std::is_same<U, T>::value, U >::type
        bar()
    {
        return mObj;
    }
private:
    T mObj;
};

template<typename T>
void bar2()
{
    Foo<int, float, double> list;
    list.bar<T>();
}

int main()
{
    bar2<float>();
    return 0;
}

除 Clang 和 Visual Studio 2015 外,这非常有效。尝试了 MSVC 19.0 和 19.10,并给出了错误:

Compiled with  /EHsc /nologo /W4 /c
main.cpp
main.cpp(30): error C2672: 'Foo<int,float,double>::bar': no matching overloaded function found
main.cpp(35): note: see reference to function template instantiation 'void bar2<float>(void)' being compiled
main.cpp(30): error C2770: invalid explicit template argument(s) for 'std::enable_if<std::is_same<U,T>::value,U>::type Foo<int,float,double>::bar(void)'
        with
        [
            T=int
        ]
main.cpp(18): note: see declaration of 'Foo<int,float,double>::bar'

GCC 至少在 4.7-6.3 之间编译这个没问题。我首先认为这可能是因为 Visual Studio 2015 中缺少 c++11 的某些功能,但令人惊讶的是,这在较旧的 Visual Studio 2013 (MSVC 18.0) 中编译得很好。 Clang 也失败了。

所以我的问题是,这是这些编译器的缺点还是我在这里做的事情是不允许的?

如果我用像 list.bar<int>() 这样的硬编码类型调用“bar”它在所有经过测试的编译器上编译。

最佳答案

使用enable_if在这里,你需要提供一个替代选项,当is_same<U, T>::value == false .理想情况下,这可以做到 by exposing all base class members bar with a using-declaration ...

using Foo<Ts...>::template bar;

不幸的是,标准禁止这样做,并且it was decided not to rectify this .因此,我们必须以另一种方式暴露它们。因此,最简单的解决方案是为 Foo<Ts...>::template bar() 制作一个包装器,如下:

template<typename T, typename... Ts>
class Foo<T, Ts...> : public Foo<Ts...>
{
public:
    // using Foo<Ts...>::template bar; // Sadly, this is forbidden.

    template<typename U>
    typename std::enable_if<std::is_same<U, T>::value, U >::type
        bar()
    {
        return mObj;
    }

    // Additional wrapper, to compensate for lack of valid syntax.
    template<typename U>
    typename std::enable_if<!std::is_same<U, T>::value, U >::type
        bar()
    {
        return Foo<Ts...>::template bar<U>();
    }

private:
    T mObj;
};

但是请注意,包装器不能调用 Foo<Ts...>::bar() , 由于它返回 void .假设这是一般情况,打算在 U 时使用不是该包的成员,有两种方法可以纠正此问题:

  • 修改Foo<Ts...>::bar() .

    template<typename... Ts>
    class Foo
    {
    public:
        template<typename T>
        T bar()
        {
            // Return an invalid value.
            return T{-1};
        }
    };
    
  • 提供第三个版本Foo<T, Ts...>::bar() , 在 U 时使用不是 T, Ts... 的成员;这个返回Foo<Ts...>::bar() .为此,定义一个特征来检测它是否在包中会很有用。

    template<typename...>
    struct is_in_pack : std::false_type {};
    
    template<typename U, typename T1, typename... Ts>
    struct is_in_pack<U, T1, Ts...> :
        std::integral_constant<bool,
                               std::is_same<U, T1>::value ||
                               is_in_pack<U, Ts...>::value>
    {};
    

    然后,我们只需要使用特征。

    template<typename T, typename... Ts>
    class Foo<T, Ts...> : public Foo<Ts...>
    {
    public:
        // using Foo<Ts...>::template bar; // Sadly, this is forbidden.
    
        template<typename U>
        typename std::enable_if<std::is_same<U, T>::value, U >::type
            bar()
        {
            return mObj;
        }
    
        // Additional wrapper, to compensate for lack of valid syntax.
        // U is a member of <T, Ts...>.
        template<typename U>
        typename std::enable_if<!std::is_same<U, T>::value &&
                                is_in_pack<U, T, Ts...>::value, U >::type
            bar()
        {
            return Foo<Ts...>::template bar<U>();
        }
    
        // Additional wrapper, to compensate for lack of valid syntax.
        // U isn't a member of <T, Ts...>.
        template<typename U>
        typename std::enable_if<!is_in_pack<U, T, Ts...>::value>::type
            bar()
        {
            return Foo<>::bar();
        }
    
    private:
        T mObj;
    };
    

在这些选项中,我建议使用后者,因为它更符合您当前的代码。

Simple test .
Convoluted test .

关于c++ - 在 vs2015 的可变参数模板中找不到重载的成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42684405/

相关文章:

c++ - 我们什么时候应该使用方法重载与具有不同命名的方法

html - 将静态网站迁移到 TYPO3 的最简单方法(HTML 模板?)

asp.net - IIS Express localhost 对于第一个请求加载速度非常慢

c++ - 将列表插入 vector 的末尾

c++ - 如果缩小分配的内存大小,还要检查 realloc() 吗?

python 瓶模板不格式化

c++ - 如何使用模板为 C++ 类创建 "calculated attribute"

ios - 从 Visual Studio 2015 构建适用于 iOS 的 Cordova 应用程序时如何解锁钥匙串(keychain)

c# - 调试器监 window 口中的 VS2015 LINQ

C++ Stringstream : Accepts a string, 但不是存储在变量中的字符串。为什么?