admin管理员组文章数量:1279236
I have templated class which has a function (myfunc()
) which does the same on every case of T
except some cases (eg. bool
). I have a working solution, based on this question:
template <class T> class opt_arg{
private: void myfunc(){
/*Do generic stuff */
}
/* Can I insert here the bool specialization? */
};
/* The specialization "inserted outside of the class body": */
template<>
inline void opt_arg<bool>::myfunc(){
/* Do bool specific stuff*/
}
As I have mentioned, it is working fine. I am just wondering, that can I insert the function specialization inside the "class body"?
I have templated class which has a function (myfunc()
) which does the same on every case of T
except some cases (eg. bool
). I have a working solution, based on this question:
template <class T> class opt_arg{
private: void myfunc(){
/*Do generic stuff */
}
/* Can I insert here the bool specialization? */
};
/* The specialization "inserted outside of the class body": */
template<>
inline void opt_arg<bool>::myfunc(){
/* Do bool specific stuff*/
}
As I have mentioned, it is working fine. I am just wondering, that can I insert the function specialization inside the "class body"?
Share Improve this question edited Feb 25 at 10:32 wohlstad 29.2k16 gold badges59 silver badges90 bronze badges asked Feb 25 at 10:21 Tom SolidTom Solid 2,4861 gold badge16 silver badges34 bronze badges 2- 1 If I get it right, I think you can't under timsong-cpp.github.io/cppwp/n4861/temp.expl.spec#5: " may be explicitly specialized for a class specialization that is implicitly instantiated; in this case, the definition of the class template shall precede the explicit specialization for the member of the class template" – Oersted Commented Feb 25 at 10:53
- 2 I feel compelled to amend my previous comment (if it was correct). The impossibility I mentioned would be with a similar syntax. However, functionally, there is indeed the solution proposed by @wohlstad. – Oersted Commented Feb 25 at 11:43
1 Answer
Reset to default 16From C++17 you can use if constexpr
(which is resolved at compile time) to achieve a similar result:
template <class T> class opt_arg {
private:
void myfunc() {
if constexpr (std::same_as<T, bool>) {
// specialized stuff
}
else {
// generic stuff
}
}
};
Live demo
As @JeremyRichards commented below, with if constexpr
in this case the code in the false branch might not even compile (thanks to the compile time resolve) which can be useful.
More info about this can be found here: Why does the false branch of "if constexpr" get compiled? (summary: this is true only for if constexpr
within a template, and the code should anyway not be ill-formed).
Note the usage of std::same_as
concept which is available from C++20. For C++17, you can use the type trait std::is_same
(and the helper std::is_same_v
).
You can add more if constexpr
branches for other types if you need (or treat several types in the same branch).
This way the logic for both cases is in one place, similarly to what you wanted.
本文标签: cSpecialization of member function from a templated class inside the class bodyStack Overflow
版权声明:本文标题:c++ - Specialization of member function from a templated class inside the class body - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741212327a2359370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论