如何在类命名空间中使用常量作为数组和模板参数
How to use constant in class namespace as the array and template parameters?
我想使用类命名空间中的常量作为静态数组的大小和另一个类中的模板参数。我有以下错误
// MS VS 2010 C++98
// A.h
class A
{
public:
const static int someNumber;
};
// A.cpp
#include <A.h>
const int A::someNumber = 5;
// B.h
#include <A.h>
class B
{
std::bitset<A::someNumber> btst; // Error! C2975: '_Bits' : invalid template argument
double control[A::someNumber]; // Error! C2466: cannot allocate an array of constant size 0
};
我怎样才能避免它们?
在 C++98 中,您不能使用 constexpr
,因为它直到 C++11 才引入。
double control[A::someNumber]
需要编译时可评估,并且您对A::someNumber
的定义驻留在不同的翻译单元中。这就是编译器所抱怨的。
但是,您可以使用enum
.我从模板元编程中使用的习语中借用了这种技术:
class A
{
public:
enum {someNumber = 5};
}; /*and note this semicolon*/
你的问题不是那么简单,因为如果你把所有的声明放在一个文件中,它将正确编译和运行。
但是只要看看编译器在编译 B.h 时可以看到什么(假设它包含在 main.cpp 或 B.cpp 中):
#include "A.h"
:好的,它包含const static int someNumber;
someNumber 是一个常量整数,其值将在链接时给出
std::bitset<A::someNumber> btst
:好的,一个位集,大小是A::someNumber
,它被声明为常量整数,直到这里很好......但是哇,当时编译器不知道常量的值!假设它是 0=> 并且您会收到两个错误,因为编译器无法知道A::someNumber
的未来值!
现在我们知道,修复很简单:只需在 A.h 中写:
const static int someNumber = 5; // valid for a litteral const
因为现在编译器在编译时知道A::someNumber
的值,并且可以正确编译包括 B.h 在内的文件。
编辑
您可能会对用A.h
编写const static int someNumber = 5;
的想法感到害怕,因为它可以包含在许多编译单元中,并且您不想在许多单元中定义相同的常量。但实际上这不是问题:
- 在编译时,编译器随时使用
A::someNumber
的值。它指出类 A 包含一个名为someNumber
的静态字段。 - 在链接时,链接器确保只有一个
A::someNumber
实例。您可以通过打印来自不同编译单元的静态 const 字段的地址来控制它,您将获得相同的值......除非你的开发工具严重损坏!
- C++函数模板需要 &for 数组参数
- C++ 数组参数不起作用
- 使用数组参数进行函数专用化
- 为什么我的数组参数的方法无法正常工作?(C )
- SWIG - Java 代理类数组参数
- 在 gcc 中使用数组参数编译外部"c"代码
- 从 VB6 调用的 C++ DLL 函数中的输出数组参数
- 如何根据数组参数项类型重载 IDL 中的函数
- 引用谷歌测试/模拟框架中的数组参数
- C++将数组参数传递给函数
- 字符星形数组参数未正确终止
- 如何将德尔菲的"字符串数组"参数翻译成C++?
- 为什么 gcc 无法推断数组参数的模板化大小?(C++11)
- 使用 const char 数组参数分析 constexpr 显示运行时执行
- C++指针数组参数
- 固定大小的多维数组参数
- 为什么数组参数被视为常量数组
- 如何在pgsql中格式化数组参数
- C++字符数组参数在x86编译中包含奇怪的字符
- 对象数组参数 - 错误:字段“字母”的类型不完整