class Myclass
{
template <typename T>
static T func()
{
T obj;
return obj;
}
template<>
static int func<int>()
{
}
};
我写了上面的类并试图编译它。 我得到了以下错误:仅用于头文件中的静态模板化成员函数的模板行为
error: explicit specialization in non-namespace scope 'class Myclass'
error: template-id 'func' in declaration of primary template
然后我搬出我的静态函数出方的类是这样的:
namespace helper
{
template <typename T>
static T func()
{
T obj;
return obj;
}
template<>
static int func<int>()
{
}
}
class Myclass
{
template <typename T>
static T func()
{
helper::func<T>();
}
};
我得到了以下错误:
error: explicit template specialization cannot have a storage class
In static member function 'static T Myclass::func()':
那么当然我内联我的专门功能,它的工作。
namespace helper
{
template <typename T>
static T func()
{
T obj;
return obj;
}
template<>
inline int func<int>()
{
return 1;
}
}
class Myclass
{
template <typename T>
static T func()
{
helper::func<T>();
}
};
我的问题是:
1)我们为什么不能专注的类中的静态成员函数。
2)为什么我们不能拥有静态模板专用功能
@tobi如果你经历了整个故事,你会发现我不得不放弃帮手,因为我无法专注于我的静态成员函数。 –
对不起。我读到这一切,但不知何故,当我达成目标时,我已经忘记了开始。对不起); – user463035818
我认为这里的混乱部分是术语“命名空间范围”。这并不意味着“在用'namespace'声明的块内部”;它意味着“不在类,结构,联合,功能等内”。该文件的开头位于命名空间范围内,因为您放置的任何内容都将放在*全局命名空间*中,而不是类或其他东西。 –