gpt4 book ai didi

c++ - 为什么不允许成员函数的模板特化?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:44:45 25 4
gpt4 key购买 nike

如果我有一些琐碎的事情,例如(澄清一下,我并不是说这是一个很好的实现,只是一个演示成员函数部分模板特化失败的例子):

template <typename T, typename U>
class BankAccount
{
T money;
U interestRate;
public:
BankAccount(T money, U interestRate) :
money(money), interestRate(interestRate)
{}

void showMeTheMoney();
};

不能能够通过以下方式专门化每个功能:

// invalid code
template <typename U>
void BankAccount <int, U>::showMeTheMoney()
{
printf("$%d.00 interest: %f\n", money, interestRate);
}

template <typename U>
void BankAccount <long, U>::showMeTheMoney()
{
printf("$%l.00 interest: %f\n", money, interestRate);
}

template <typename U>
void BankAccount <float, U>::showMeTheMoney()
{
printf("$%.2f interest: %f\n", money, interestRate);
}

template <typename U>
void BankAccount <double, U>::showMeTheMoney()
{
printf("$%.2f interest: %f\n", money, interestRate);
}

int main(int argc, char *argv[])
{
BankAccount<double, float> b(500, 0.03);
b.showMeTheMoney();
BankAccount<std::uint64_t, float> c(1234, 0.01);
c.showMeTheMoney();
}

等不幸的是,C++ 标准不允许这样做:

14.5.5.3.1. The template parameter list of a member of a class template partial specialization shall match the template parameter list of the class template partial specialization. The template argument list of a member of a class template partial specialization shall match the template argument list of the class template partial specialization.

因此,唯一的解决方案(据我所知)是使用类型特征或使用样板代码重现整个类。这个决定背后是否有理由,或者这是 C++ 中根本不存在的东西,因为没有足够的需求,还是其他一些原因?

最佳答案

因为它不是您编写的成员函数的模板特化。它是类(class)的特化。因此,代码应该如下所示(我添加了一个公共(public)基类,您不必为所有规范定义成员):

template <typename T, typename U>
class BankAccountBase
{
protected:
T money;
U interestRate;

public:
BankAccountBase(T money, U interestRate) :
money(money), interestRate(interestRate)
{}
};

template <typename T, typename U>
class BankAccount
{
T money;
U interestRate;
public:
BankAccount(T money, U interestRate) :
money(money), interestRate(interestRate)
{}

void showMeTheMoney();
};

template <typename U>
class BankAccount <int, U> : public BankAccountBase <int, U>
{
public:
BankAccount(int money, U interestRate) :BankAccountBase(money, interestRate) { }
void showMeTheMoney();
};

template <typename U>
class BankAccount <long, U> : public BankAccountBase <long, U>
{
public:
BankAccount(long money, U interestRate) :BankAccountBase(money, interestRate) { }
void showMeTheMoney();
};
template <typename U>
class BankAccount <float, U> : public BankAccountBase <float, U>
{
BankAccount(float money, U interestRate) :BankAccountBase(money, interestRate) { }
public:
void showMeTheMoney();
};
template <typename U>
class BankAccount <double, U> : public BankAccountBase <double, U>
{
public:
BankAccount(double money, U interestRate) :BankAccountBase(money, interestRate) { }
void showMeTheMoney();
};

template <typename U>
class BankAccount <long long, U> : public BankAccountBase <long long, U>
{
public:
BankAccount(long long money, U interestRate) :BankAccountBase(money, interestRate) { }
void showMeTheMoney();
};


// invalid code
template <typename U>
void BankAccount <int, U>::showMeTheMoney()
{
printf("$%d.00 interest: %f\n", money, interestRate);
}

template <typename U>
void BankAccount <long, U>::showMeTheMoney()
{
printf("$%l.00 interest: %f\n", money, interestRate);
}

template <typename U>
void BankAccount <long long, U>::showMeTheMoney()
{
printf("$%l.00 interest: %f\n", money, interestRate);
}

template <typename U>
void BankAccount <float, U>::showMeTheMoney()
{
printf("$%.2f interest: %f\n", money, interestRate);
}

template <typename U>
void BankAccount <double, U>::showMeTheMoney()
{
printf("$%.2f interest: %f\n", money, interestRate);
}

int main(int argc, char *argv[])
{
BankAccount<double, float> b(500, 0.03);
b.showMeTheMoney();
BankAccount<long long, float> c(1234, 0.01);
c.showMeTheMoney();
}

关于c++ - 为什么不允许成员函数的模板特化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53855546/

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