如何在不依赖#define 的情况下转发声明数组大小?
How to forward declare array size without relying on #define?
我想构建一个作用于 vectors/matrices 固定大小的库(即函数集合)。我想在主文件中指定这个固定大小,而不触及库。我可以使用 #define
来做到这一点,但我认为这种方法是一种 hack,而不是“正确”的方法。我还有哪些其他方法可以实现这个目标?思路是写一次库,以后再也不碰,甚至不看一遍。
这是下面的玩具示例,program.cpp
//program.cpp -- Definition of my_external_constant here
const unsigned int array_size = 10;
#define my_external_constant array_size // I am looking for alternatives.
#include "helper.h"
int main(int argc, char* argv[]) {
test_function1();
unsigned int X[array_size];
X[0]=500;X[1]=0;X[2]=9;X[3]=111;X[4]=0;
print_vector(X,3);
return 0;
}
然后helper.h
//helper.h
#ifndef HELPER_H
#define HELPER_H
#include<iostream>
#ifndef my_external_constant // I want to avoid this...
#define my_external_constant 1 //...and this...
#endif //...and this.
void test_function1(void);
//The library uses helper_max_size everywhere, and its value is inherited from outside.
const unsigned int helper_max_size = my_external_constant;
void print_vector(unsigned int[helper_max_size], const unsigned int);
#endif /* HELPER_H */
并执行helper.cpp
//helper.cpp
#include "helper.h"
void test_function1(void) {
std::cout << "Hello world!\n";
return;
}
void print_vector(unsigned int my_vector[helper_max_size], const unsigned int my_size) {
for (unsigned int i=0;i<my_size;++i) {
std::cout << my_vector[i] << " ";
}
std::cout << "\n";
return;
}
该程序应仅包含一个翻译单元,即应按以下行编译:
g++ -o program program.cpp helper.h helper.cpp
相关:
How do I use extern to share variables between source files?
Declare array size in header file without #define's
I want to specify this fixed size in the main file, without touching the library.
The idea is to write a library once, and then never to touch, or even look at it again.
将库中的所有实用程序和函数参数化为数组的大小。示例:
// helper.h
template <std::size_t N>
void print_vector(unsigned int(&array)[N], const unsigned int) { /* ... */ }
// program.cpp
#include <cstddef>
#include "helper.h"
constexpr std::size_t array_size = 10;
int main(int argc, char* argv[])
{
test_function1();
unsigned int X[array_size];
X[0]=500;X[1]=0;X[2]=9;X[3]=111;X[4]=0;
print_vector(X, 3);
}
在现代 C++ 中,您应该更喜欢 std::array
而不是 C 风格的数组。如果您需要可调整大小的数组,您可能还想使用 std::vector
。例如:
// helper.h
template <std::size_t N>
void print_vector(const std::array<unsigned int, N>& array, const unsigned int)
{
/* ... */
}
// program.cpp
#include <cstddef>
#include "helper.h"
constexpr std::size_t array_size = 10;
int main(int argc, char* argv[])
{
test_function1();
std::array<unsigned int, array_size> X;
X[0]=500;X[1]=0;X[2]=9;X[3]=111;X[4]=0;
print_vector(X, 3);
}
我想构建一个作用于 vectors/matrices 固定大小的库(即函数集合)。我想在主文件中指定这个固定大小,而不触及库。我可以使用 #define
来做到这一点,但我认为这种方法是一种 hack,而不是“正确”的方法。我还有哪些其他方法可以实现这个目标?思路是写一次库,以后再也不碰,甚至不看一遍。
这是下面的玩具示例,program.cpp
//program.cpp -- Definition of my_external_constant here
const unsigned int array_size = 10;
#define my_external_constant array_size // I am looking for alternatives.
#include "helper.h"
int main(int argc, char* argv[]) {
test_function1();
unsigned int X[array_size];
X[0]=500;X[1]=0;X[2]=9;X[3]=111;X[4]=0;
print_vector(X,3);
return 0;
}
然后helper.h
//helper.h
#ifndef HELPER_H
#define HELPER_H
#include<iostream>
#ifndef my_external_constant // I want to avoid this...
#define my_external_constant 1 //...and this...
#endif //...and this.
void test_function1(void);
//The library uses helper_max_size everywhere, and its value is inherited from outside.
const unsigned int helper_max_size = my_external_constant;
void print_vector(unsigned int[helper_max_size], const unsigned int);
#endif /* HELPER_H */
并执行helper.cpp
//helper.cpp
#include "helper.h"
void test_function1(void) {
std::cout << "Hello world!\n";
return;
}
void print_vector(unsigned int my_vector[helper_max_size], const unsigned int my_size) {
for (unsigned int i=0;i<my_size;++i) {
std::cout << my_vector[i] << " ";
}
std::cout << "\n";
return;
}
该程序应仅包含一个翻译单元,即应按以下行编译:
g++ -o program program.cpp helper.h helper.cpp
相关:
How do I use extern to share variables between source files?
Declare array size in header file without #define's
I want to specify this fixed size in the main file, without touching the library.
The idea is to write a library once, and then never to touch, or even look at it again.
将库中的所有实用程序和函数参数化为数组的大小。示例:
// helper.h
template <std::size_t N>
void print_vector(unsigned int(&array)[N], const unsigned int) { /* ... */ }
// program.cpp
#include <cstddef>
#include "helper.h"
constexpr std::size_t array_size = 10;
int main(int argc, char* argv[])
{
test_function1();
unsigned int X[array_size];
X[0]=500;X[1]=0;X[2]=9;X[3]=111;X[4]=0;
print_vector(X, 3);
}
在现代 C++ 中,您应该更喜欢 std::array
而不是 C 风格的数组。如果您需要可调整大小的数组,您可能还想使用 std::vector
。例如:
// helper.h
template <std::size_t N>
void print_vector(const std::array<unsigned int, N>& array, const unsigned int)
{
/* ... */
}
// program.cpp
#include <cstddef>
#include "helper.h"
constexpr std::size_t array_size = 10;
int main(int argc, char* argv[])
{
test_function1();
std::array<unsigned int, array_size> X;
X[0]=500;X[1]=0;X[2]=9;X[3]=111;X[4]=0;
print_vector(X, 3);
}