我想成为一个功能模板,并希望尽可能地限制模板类型。
以下是较大层次结构的摘要,T
中template <class T> void Play(T&);
的格式可以是T
或T<U>
。如果是T<U>
,这意味着T是一个类模板,我想成为T<U>
专用的函数。
以下代码段的预期行为是成功编译/链接/执行而不产生输出This should not be printed
。
#include <iostream>
enum class Genre { Rock = 111, Pop = 999 };
/* this is the global interface: */
template <class T> void Play(T&);
template <Genre genre> class Song {
/* befriend own player */
template <class T> friend void Play(Song<genre>&);
private:
int v = int(genre);
};
/* desired function resolution: */
template <Genre genre> void Play(Song<genre>& d)
{
std::cout << "Genre: " << d.v << std::endl;
}
template <class T> void Play(T& d)
{
std::cout << "This should not be printed" << std::endl;
}
/* these two functions are not desired but I tried... */
template<> inline void Play(Song<Genre::Pop>& d)
{ Play<Genre::Pop>(d); }
template<> inline void Play(Song<Genre::Rock>& d)
{ Play<Genre::Rock>(d); }
int main(int argc, char *argv[]) {
Song<Genre::Pop> s;
Song<Genre::Rock> p;
Play<decltype(s)>(s);
Play(s);
Play(p);
return 0;
}
答案 0 :(得分:2)
我在这里有两个问题我可以识别:你的friend
声明正在取消错误的功能而你的两个声明正在发挥作用。函数以递归方式调用自己。
要解决第一个问题,我们需要告诉编译器Play
函数在开始查看Song
类之前是一个模板:
/* this is the global interface: */
//need to forward declare the Song template class
template <Genre genre> class Song;
//forward declare the version of Play templated on Genre
template <Genre genre>
void Play(Song<genre>&);
//keep the version you had originally
template <typename T>
void Play(T&);
template <Genre genre> class Song {
/* befriend own player */
//now picks up the correct function
friend void Play <> (Song<genre>&);
private:
int v = int(genre);
};
对于转发功能,我们需要对template <typename T> Play(T&>
版本进行完全特化:
template <>
void Play<Song<Genre::Pop>> (Song<Genre::Pop>& d)
{ Play(d); }
template <>
void Play<Song<Genre::Rock>> (Song<Genre::Rock>& d)
{ Play(d); }
另一种方法是在传入Song
时进行类型特征检查,然后使用SFINAE启用/禁用该功能:
template <class T>
struct is_song : std::false_type {};
template <Genre genre>
struct is_song<Song<genre>> : std::true_type {};
template <typename T, std::enable_if_t<is_song<T>::value>* = nullptr>
void Play (T& d)
{ Play(d); }
template <typename T, std::enable_if_t<!is_song<T>::value>* = nullptr>
void Play(T& d)
{
std::cout << "This should not be printed" << std::endl;
}
现在一切正常! Demo
答案 1 :(得分:1)
您可以这样做:
#include <iostream>
enum class Genre : int { Rock = 111, Pop = 999 };
template<Genre genre> class Song;
/* this is the global interface: */
template <Genre genre> void Play(Song<genre>&);
template <Genre genre>
class Song
{
/* befriend own player */
friend void Play<>(Song<genre>&);
private:
int v = int(genre);
};
/* desired function resolution: */
template <Genre genre>
void Play(Song<genre>& d)
{
std::cout << "Genre: " << d.v << std::endl;
}
/* non-desired function */
template <class T>
void Play(T& d)
{
std::cout << "This should not be printed" << d.v << std::endl;
}
int main(int argc, char *argv[])
{
Song<Genre::Pop> s;
Song<Genre::Rock> p;
//Play<decltype(s)>(s); // <--- will not compile: calls the 'non-desired' function
// <--- which is not friend of Song<Genre::Pop>
// <--- and compilation fails as the function tries to access the private member v
Play(s);
Play(p);
//Play<Genre::Rock>(s); // <--- will also not compile
return 0;
}
此处Play
仅为friend
个Song
的特定&#39;} genre
。