我可以对类模板的功能模板成员进行单独的定义?
这是一个最小的代码示例,可以显示我正在尝试的工作,但不是我想要的:
#include <string>
#include <type_traits>
#include <iostream>
struct string_tag {
using R=const std::string;
};
struct int_tag {
using R=const int;
};
template <bool TS>
class Wibble {
public:
template<typename TAG>
typename TAG::R getValue(TAG);
};
template <bool TS>
template <typename TAG>
typename TAG::R Wibble<TS>::getValue(TAG) {
if constexpr (std::is_same<TAG, string_tag>::value) {
return "hello";
}
if constexpr (std::is_same<TAG, int_tag>::value) {
return 42;
}
}
// instantiate classes
template class Wibble<true>;
template class Wibble<false>;
int main () {
Wibble<true> tw;
Wibble<false> fw;
std::cout << "tw string: " << tw.getValue(string_tag{}) << std::endl;
std::cout << "tw int: " << tw.getValue(int_tag{}) << std::endl;
std::cout << "fw string: " <<fw.getValue(string_tag{}) << std::endl;
std::cout << "fw int: " << fw.getValue(int_tag{}) << std::endl;
}
我要更改的部分是所有constexpr
逻辑的丑陋函数模板定义。我希望能够在tag
独立的情况下定义不同的专业,但是这样做可以使我重新定义...
errors。
语法像以下内容一样好:
template<bool TS>
template<>
string_tag::R Wibble<TS>::getValue(string_tag) {
return "hello";
}
但是计算机说“不”。
Here's a minimal code example to show what I'm attempting that works, but not how I'd like it to:
#include <string>
#include <type_traits>
#include <iostream>
struct string_tag {
using R=const std::string;
};
struct int_tag {
using R=const int;
};
template <bool TS>
class Wibble {
public:
template<typename TAG>
typename TAG::R getValue(TAG);
};
template <bool TS>
template <typename TAG>
typename TAG::R Wibble<TS>::getValue(TAG) {
if constexpr (std::is_same<TAG, string_tag>::value) {
return "hello";
}
if constexpr (std::is_same<TAG, int_tag>::value) {
return 42;
}
}
// instantiate classes
template class Wibble<true>;
template class Wibble<false>;
int main () {
Wibble<true> tw;
Wibble<false> fw;
std::cout << "tw string: " << tw.getValue(string_tag{}) << std::endl;
std::cout << "tw int: " << tw.getValue(int_tag{}) << std::endl;
std::cout << "fw string: " <<fw.getValue(string_tag{}) << std::endl;
std::cout << "fw int: " << fw.getValue(int_tag{}) << std::endl;
}
The part I want to change is the ugly function template definition with all the constexpr
logic. I'd like to be able to define the different specializations in TAG
standalone, but doing this gives me redefinition of ...
errors.
Syntax like the following would be nice:
template<bool TS>
template<>
string_tag::R Wibble<TS>::getValue(string_tag) {
return "hello";
}
but computer says "no".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想到了一个想法,阅读语言规格等。我想到了以下内容:
类模板必须专业化才能专门化会员函数模板。时期。这不能用概念克服,或者至少我还没有找到方法。我想您不想为每种TS复制代码。也许可以使用一些Alexandrescu风格的元编程技术自动完成,但是我想不到任何事情。
超载而不是模板是一个不错的选择,但我猜您希望能够添加它们,而不是在班级内部...
“ noreferrer”> https://godbolt.org/z/gspk4mp8m
希望它有帮助。
I gave it a thought, read language specs etc. and the following things come to my mind:
Class template has to be specialized in order to specialize member function template. Period. This cannot be overcome with concepts, or at least I haven't found a way. I guess you don't want to replicate the code for each case of TS. Maybe it can be done automagically with some Alexandrescu-style metaprogramming techniques, but I can't think of anything right off the bat.
Overloads instead of templates are a good alternative but I'm guessing you'd like to be able to add them out-of-line, instead of inside the class...
Then I recalled David Wheeler: “All problems in computer science can be solved by another level of indirection." So let's add one:
https://godbolt.org/z/GsPK4MP8M
Hope it helps.