template<typename T>
class CConstraint
{
public:
CConstraint()
{
}
virtual ~CConstraint()
{
}
template <typename TL>
void Verify(int position, int constraints[])
{
}
template <>
void Verify<int>(int, int[])
{
}
};
Run Code Online (Sandbox Code Playgroud)
在g ++下编译它会产生以下错误:
非命名空间范围'类CConstraint'中的显式特化
在VC中,它编译得很好.任何人都可以让我知道解决方法吗?
请看看我想做什么:
#include <iostream>
namespace first
{
template <class T>
class myclass
{
T t;
public:
void who_are_you() const
{ std::cout << "first::myclass"; }
};
}
namespace second
{
using first::myclass;
template <>
class myclass <int>
{
int i, j;
public:
void who_are_you() const
{ std::cout << "second::myclass"; }
};
}
Run Code Online (Sandbox Code Playgroud)
这是不允许的.请问,请说明为什么专业化不能在不同的命名空间中,以及有哪些可用的解决方案?此外,它是在C++ 0x中修复的东西吗?
这将让我举例来说,专注std::max,std::swap,std::numeric_limits,等.不加东西诉诸未定义行为::std::?
@AndreyT这是我如何使用它:
// my_integer is a class
std::numeric_limits<my_integer>::max(); // specialized std::numeric_limits for my_integer
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?
在找到关于stackoverflow的许多问题的答案之后,我现在遇到了一个我无法找到答案的问题,我希望有人愿意帮助我!
我的问题是我想在C++中对一个类中的函数进行明确的模板化.我的编译器(g ++)和C++标准(§14.7.3)中的一个看起来告诉我,这个特化必须在声明类的命名空间中完成.我明白这意味着我不能把专业化放在课堂里,但是我没有看到这个限制的重点!有没有人知道是否有充分的理由不让专业在课堂上进行?
我知道有一些解决方法,例如将函数放在结构体中,但我想理解为什么语言有这种设计.如果有充分的理由不在课堂上允许专门的功能,我想在尝试解决它之前我应该知道它.
提前致谢!
为了让我的问题更加精确:以下是一些测试示例中的代码,说明了我想要做的事情:
#include <cstdio>
namespace MalinTester {
template <size_t DIMENSIONALITY>
class SpecializationTest {
public:
SpecializationTest() {
privateVariable = 5;
};
virtual ~SpecializationTest() {};
void execute() {
execute<DIMENSIONALITY>();
};
private:
int privateVariable;
template <size_t currentDim>
static void execute() {
printf("This is the general case. Current dim is %d. The private variable is %d.\n", currentDim, privateVariable);
execute<currentDim-1>();
}
template <>
static void execute<0>() {
printf("This is the base case. Current dim is 0.\n");
}
};
Run Code Online (Sandbox Code Playgroud)
这是不可能的; g ++说: …