gpt4 book ai didi

c++ - 模板类的类成员的特化

转载 作者:行者123 更新时间:2023-12-01 14:43:43 27 4
gpt4 key购买 nike

A为模板类,其中包含一个内部struct。我想根据A的模板参数专门化内部结构(并且仅此而已)。以下代码似乎可以正确完成此工作:

#include <iostream>

template <bool rgb>
struct A {
struct colors;
A();
};

template <>
struct A<true>::colors { enum : std::size_t { red, green, blue }; };

template <>
struct A<false>::colors { enum : std::size_t { cyan, magenta, yellow, black }; };

template<bool rgb>
A<rgb>::A()
{
if (rgb) {
std::cout << "rgb true" << std::endl;
}
else {
std::cout << "rgb false" << std::endl;
}
}

int main()
{
using colors_true = A<true>::colors;
using colors_false = A<false>::colors;

A<true> at{};
A<false> af{};
std::cout << colors_true::red << std::endl;
std::cout << colors_false::yellow << std::endl;
}

看到它 live on Coliru

( A的构造函数只是为了说明我只是专门研究 A::colors)

现在,考虑 A包含附加模板参数的情况。我想模仿上面的代码,只专门设置 bool参数。但是,以下代码无法编译:
#include <iostream>

template <bool rgb, int i>
struct A {
struct colors;
A();
};

template <int i>
struct A<true, i>::colors { enum : std::size_t { red, green, blue }; };

template <int i>
struct A<false, i>::colors { enum : std::size_t { cyan, magenta, yellow, black }; };

template<bool rgb, int i>
A<rgb, i>::A()
{
if (rgb) {
std::cout << "rgb true";
}
else {
std::cout << "rgb false";
}
std::cout << " i = " << i << std::endl;
}

int main()
{
using colors_true = A<true, 2>::colors;
using colors_false = A<false, 5>::colors;

A<true, 2> at{};
A<false, 5> af{};
std::cout << colors_true::red << std::endl;
std::cout << colors_false::yellow << std::endl;
}

看到它 live on Coliru

编译错误为:
main.cpp:10:20: error: invalid class name in declaration of 'class A<true, i>::colors'

10 | struct A<true, i>::colors { enum : std::size_t { red, green, blue }; };

| ^~~~~~

main.cpp:13:21: error: invalid class name in declaration of 'class A<false, i>::colors'

13 | struct A<false, i>::colors { enum : std::size_t { cyan, magenta, yellow, black }; };

| ^~~~~~
A的部分特化,如以下代码所示
#include <iostream>

template <bool rgb, int i>
struct A {
struct colors;
A();
};

template <int i>
struct A<true, i> {
struct colors { enum : std::size_t { red, green, blue }; };
};

template <int i>
struct A<false, i> {
struct colors { enum : std::size_t { cyan, magenta, yellow, black }; };
};

template<bool rgb, int i>
A<rgb, i>::A()
{
if (rgb) {
std::cout << "rgb true";
}
else {
std::cout << "rgb false";
}
std::cout << " i = " << i << std::endl;
}

int main()
{
using colors_true = A<true, 2>::colors;
using colors_false = A<false, 5>::colors;

A<true, 2> at{};
A<false, 5> af{};
std::cout << colors_true::red << std::endl;
std::cout << colors_false::yellow << std::endl;
}

也不起作用。看到它 live on Coliru。在代码编译时, A的部分特化完全掩盖了构造函数 A::A(),如输出所示。换句话说,在上面的代码中,编译器选择了 A的两个部分专用的版本,其中未明确定义构造函数。

作为一种解决方法,我发现可以使用继承:
#include <iostream>

template <bool rgb>
struct colors_type;

template <>
struct colors_type<true> {
struct colors { enum : std::size_t { red, green, blue }; };
};

template <>
struct colors_type<false> {
struct colors { enum : std::size_t { cyan, magenta, yellow, black }; };
};

template <bool rgb, int i>
struct A : public colors_type<rgb> {
A();
};

template<bool rgb, int i>
A<rgb, i>::A()
{
if (rgb) {
std::cout << "rgb true";
}
else {
std::cout << "rgb false";
}
std::cout << " i = " << i << std::endl;
}

int main()
{
using colors_true = A<true, 2>::colors;
using colors_false = A<false, 5>::colors;

A<true, 2> at{};
A<false, 5> af{};
std::cout << colors_true::red << std::endl;
std::cout << colors_false::yellow << std::endl;
}

看到它 live on Coliru
  • 为什么仅在完全封闭化类的模板参数(第一代码)时而不对内部结构进行特殊化(第二代码),才可以对内部结构进行特殊化?
  • 除了继承技巧(第四代码)之外,还有其他解决方案吗?
  • 最佳答案

    这是指定语言的方式:

  • 您可以显式专门化类模板的成员类(请参见[temp.expl.spec])
  • 您可以为类提供部分特化(请参阅[temp.class.spec])

  • 对[temp.class.spec]感兴趣:

    A partial specialization of a class template provides an alternative definition of the template that is used instead of the primary definition

    Each class template partial specialization is a distinct template and definitions shall be provided for the members of a template partial specialization


    我认为您提供的继承技巧是最好的一种技巧,或者至少是最常用的一种技巧。

    关于c++ - 模板类的类成员的特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59588942/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com