为什么我不能在其专业化中使用模板成员或者我做错了什么?

问题描述 投票:0回答:1

这是我的代码:

#include <iostream>

template <typename T>
struct Foo
{
public:
    T DataMember;
};

template<>
struct Foo<int>
{
public:
    void bar()
    {
        std::cout << DataMember;
    }
};

当我尝试编译它时,它给出错误 C2065 'DataMember': 未声明的标识符 我想做的是在其专业化中使用模板成员

我尝试了很多东西,并在谷歌上搜索了几个小时的问题,但我找到的所有内容都是不使用模板成员的示例以及与 c++ 模板相关的其他问题,但不是我需要的。

c++ c++14 template-specialization c++-templates
1个回答
0
投票

您混淆了继承和类模板专业化的概念。当您将

Foo
专门化为
int
时,您将指示编译器完全忽略通用模板定义,而仅关注为
Foo<int>
提供的专用版本。因此
template<> struct Foo<int>
没有
DataMember
;因此编译器错误!

根据用例,您可能最终会主要

  • SFINAE(替换失败不是错误)以及模板专门化。

    #include <type_traits> // For std::enable_if
    
    // Primary template for Foo
    template <typename T, typename = void> struct Foo
    {
        T DataMember;
    };
    
    // Specialization for Foo<int> with a bar() member function
    template <typename T>
    struct Foo<T, std::enable_if_t<std::is_same<U, int>::value>* = nullptr>
    {
        T DataMember; // you have to repeat "DataMember" here!
    
        void bar()
        {
            std::cout << DataMember;
        }
    };
    
  • 或者您可以直接在主模板中使用

    std::enable_if
    (即 SFINAE),根据类型
    bar()
    有条件地启用
    T
    成员函数。

    template <typename T>
    struct Foo
    {
        T DataMember;
    
        // Enable bar() only for T being int
        template <typename U = T, std::enable_if_t<std::is_same<U, int>::value>* = nullptr>
        void bar()
        {
            std::cout << DataMember;
        }
    };
    
© www.soinside.com 2019 - 2024. All rights reserved.