模板类里面的模板函数的C++特化

Ogr*_*m33 29 c++ syntax templates template-specialization

什么是专门用于模板类中的模板函数的C++语法?例如,考虑我有以下两个类及其用法.我希望能够为不同类型提供方法X :: getAThing()的专门实现.例如:int,std :: string,任意指针或类等.

template <class c1> class X {
public:
   template<typename returnT> returnT getAThing(std::string param);
   static std::string getName();
private:
   c1 theData;
};

// This works ok...
template <class c1> std::string X<c1>::getName() {
   return c1::getName();
}

// This blows up with the error:
// error: prototype for 'int X<c1>::getAThing(std::string)' does not match any in class 'X<c1>'
template <class c1> template <typename returnT> int X<c1>::getAThing(std::string param) {
   return getIntThing(param); // Some function that crunches on param and returns an int.
}

// More specialized definitions of getAThing() for other types/classes go here...

class Y {
public:
   static std::string getName() { return "Y"; }
};

int main(int argc, char* argv[])
{
   X<Y> tester;
   int anIntThing = tester.getAThing<int>(std::string("param"));
   cout << "Name: " <<  tester.getName() << endl;
   cout << "An int thing: " << anIntThing << endl;
}
Run Code Online (Sandbox Code Playgroud)

我一直试图猜测专业化的正确语法至少一个小时,并且无法解决任何可编译的问题.任何帮助将不胜感激!

Nim*_*Nim 18

AFAIK(以及标准专家可以纠正我),你不能专门化类模板的模板化功能而不专门化类本身......

即以下我认为将起作用:

template <> template <> int X<Y>::getAThing<int>(std::string param) {
   return getIntThing(param); // Some function that crunches on param and returns an int.
}
Run Code Online (Sandbox Code Playgroud)

  • @Omnifarious,我知道你的意思,我今天被这个困了大约一个小时:'在'>'之前预期的初级表达!!?!原来我在类模板中调用函数模板时缺少关键字`template`,现在使用奇怪的语法:`some_obj.template foo <bar>();` - wtf?谁认为这是一个聪明的主意?!? (2认同)
  • @Omnifarious:14.7.3p18:“在类模板的成员或出现在命名空间范围中的成员模板的显式专业化声明中,成员模板及其一些封闭的类模板可能保持非专业化,除非声明不应如果类成员模板的封闭类模板也没有显式专门化,则显式专门化该类成员模板。” (2认同)
  • 我想我对英语有很好的把握,但这样的陈述让我眼前一亮,我避免阅读标准的原因...... (2认同)

小智 8

C++没有对函数模板进行部分特化的概念.但是,您可以通过函数重载获得与完全特化相同的效果.

我假设你有这样的东西,这实际上是唯一的方法之一.

template<class TYPE>
class MyInterface {
public:
    template<class RETURN>
    RETURN myFunction(RETURN& ref, ....);
};
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您通过声明具有所需类型的普通成员函数来专门化"myFunction()".C++的函数重载规则应该给你你想要的东西,例如

template<class TYPE>
class MyInterface {
public:
    template<class RETURN>
    RETURN myFunction(RETURN& ref, ....);

    // String specialization
    std::string myFunction(std::string& ref, ...);
};
Run Code Online (Sandbox Code Playgroud)

编译器将在适当的地方使用"std :: string"函数,并且可能永远不会使用内部模板.


Omn*_*ous 6

所以,我采取了不同的方法来回答你的问题.我将从你想要的东西开始,并且有效.然后,也许我们可以弄清楚如何将它置于更接近你真正想要的东西:

#include <string>
#include <iostream>

int getIntThing(const ::std::string &param);

template <typename returnT>
returnT getThingFree(const ::std::string &param);

template <>
int getThingFree<int>(const ::std::string &param)
{
   return getIntThing(param);
}

// More specialized definitions of getAThing() for other types/classes
// go here...

template <class c1> class X {
public:
   template<typename returnT> returnT getAThing(std::string param);
   static std::string getName();
private:
   c1 theData;
};

// This works ok...
template <class c1> std::string X<c1>::getName() {
   return c1::getName();
}

// This also works, but it would be nice if I could explicitly specialize
// this instead of having to explicitly specialize getThingFree.
template <class c1>
template <class RT>
RT X<c1>::getAThing(std::string param) {
   // Some function that crunches on param and returns an RT.
   // Gosh, wouldn't it be nice if I didn't have to redirect through
   // this free function?
   return getThingFree<RT>(param);
}

class Y {
public:
   static std::string getName() { return "Y"; }
};

int main(int argc, char* argv[])
{
   using ::std::cout;
   X<Y> tester;
   int anIntThing = tester.getAThing<int>(std::string("param"));
   cout << "Name: " <<  tester.getName() << '\n';
   cout << "An int thing: " << anIntThing << '\n';
}
Run Code Online (Sandbox Code Playgroud)

这是另一种有用的想法,并不是你想要的,但更接近.我想你自己已经想过了.它使用类型演绎的方式也相当丑陋.

#include <string>
#include <iostream>

template <class c1> class X;

int getIntThing(const ::std::string &param)
{
   return param.size();
}

// You can partially specialize this, but only for the class, or the
// class and return type. You cannot partially specialize this for
// just the return type. OTOH, specializations will be able to access
// private or protected members of X<c1> as this class is declared a
// friend.
template <class c1>
class friendlyGetThing {
 public:
   template <typename return_t>
   static return_t getThing(X<c1> &xthis, const ::std::string &param,
                            return_t *);
};

// This can be partially specialized on either class, return type, or
// both, but it cannot be declared a friend, so will have no access to
// private or protected members of X<c1>.
template <class c1, typename return_t>
class getThingFunctor {
 public:
   typedef return_t r_t;

   return_t operator()(X<c1> &xthis, const ::std::string &param) {
      return_t *fred = 0;
      return friendlyGetThing<c1>::getThing(xthis, param, fred);
   }
};

template <class c1> class X {
public:
   friend class friendlyGetThing<c1>;

   template<typename returnT> returnT getAThing(std::string param) {
      return getThingFunctor<c1, returnT>()(*this, param);
   }
   static std::string getName();
private:
   c1 theData;
};

// This works ok...
template <class c1> std::string X<c1>::getName() {
   return c1::getName();
}

class Y {
public:
   static std::string getName() { return "Y"; }
};

template <class c1>
class getThingFunctor<c1, int> {
 public:
   int operator()(X<c1> &xthis, const ::std::string &param) {
      return getIntThing(param);
   }
};

// More specialized definitions of getAThingFunctor for other types/classes
// go here...

int main(int argc, char* argv[])
{
   using ::std::cout;
   X<Y> tester;
   int anIntThing = tester.getAThing<int>(std::string("param"));
   cout << "Name: " <<  tester.getName() << '\n';
   cout << "An int thing: " << anIntThing << '\n';
}
Run Code Online (Sandbox Code Playgroud)

我会建议宣布getThingFunctorfriendlyGetThing在半私营公用事业的命名空间.