我有以下测试代码:
// friendfunction.h
#include <iostream>
template<typename T, unsigned int... TDIM> class MyClass
{
template<typename T1, typename T0, unsigned int... TDIM0, class> friend inline const MyClass<T0, TDIM0...> myFunction(const T1& x, const MyClass<T0, TDIM0...>& y);
};
template<typename T0, typename T, unsigned int... TDIM, class = typename std::enable_if<std::is_fundamental<T0>::value>::type> inline const MyClass<T, TDIM...> myFunction(const T0& x, const MyClass<T, TDIM...>& y)
{
std::cout<<"It works !"<<std::endl;
return MyClass<T, TDIM...>(y);
}
// friendfunction.cpp
#include "friendfunction.h"
int main(int argc, char *argv[])
{
MyClass<double, 3, 3> myClass;
myFunction(10, myClass);
return 0;
}在g++ (4.6.1)中,我使用以下命令进行编译:
g++ -Wall -Wextra -Winline -Wno-unused -O3 -std=c++0x friendfunction.cpp -o friendfunction和英特尔ICPC (12.1.0 20111011):
icpc -Wall -Wno-unused -O3 -std=c++0x friendfunction.cpp -o friendfunction在使用g++时,我没有收到任何错误和警告,但在使用英特尔C++时,我收到了以下警告:
friendfunction.h(5): warning #2922: template parameter "<unnamed>" cannot be used because it follows a parameter pack and cannot be deduced from the parameters of function template "myFunction"
template<typename T1, typename T0, unsigned int... TDIM0, class> friend inline const MyClass<T0, TDIM0...> myFunction(const T1& x, const MyClass<T0, TDIM0...>& y);有没有人有避免这种情况的解决方案?
此外,您是否发现friend函数的语法完全正确/安全,或者您是否认为它会在某些奇怪的情况下产生错误?
发布于 2012-08-12 21:15:06
只是猜测,但看起来icpc希望参数包成为最后一个模板参数。如果包中的可变参数不能与包后面的另一个参数区分开来,那么这是合理的,IMHO。但是在您的例子中,可变参数是整数,下一个是类类型,所以看起来icpc搞错了……
https://stackoverflow.com/questions/11917351
复制相似问题