英文:
How to use VCL as a separate namespace?
问题
我的目标是将所有vectorclass库的类型名称分离到一个单独的命名空间中,以便vcl::Vec4i会编译,但Vec4i不会。我尝试使用手册中的示例,但它不起作用。
失败的尝试按照手册:
#include <iostream>
#include "vcl/vectorclass.h"
#define VCL_NAMESPACE vcl
using namespace vcl; //error
int main() {
vcl::Vec4i vec; //error
Vec4i vec; //仍然可以编译
return 0;
}
失败信息:
root@vlad:/avx_vcl clang++ -std=c++17 -mavx -o test main.cpp
main.cpp:4:17: error: expected namespace name
using namespace vcl;
^
main.cpp:6:2: error: use of undeclared identifier 'vcl'
vcl::Vec4i vec;
^
2 errors generated.
期望的结果:
#define VCL_NAMESPACE vcl
int main() {
vcl::Vec4i vec; //编译
Vec4i vec; //不会编译
return 0;
}
我应该更改什么?
英文:
My goal is to separate all vectorclass-library typenames to a separate namespace, so that vcl::Vec4i will compile, but Vec4i won't. I tried to use example from manual, however it's not working.
Failed attempt following the manual:
#include <iostream>
#include "vcl/vectorclass.h"
#define VCL_NAMESPACE vcl
using namespace vcl; //error
int main() {
vcl::Vec4i vec; //error
Vec4i vec; //still compiles
return 0;
}
Failure message:
root@vlad:/avx_vcl clang++ -std=c++17 -mavx -o test main.cpp
main.cpp:4:17: error: expected namespace name
using namespace vcl;
^
main.cpp:6:2: error: use of undeclared identifier 'vcl'
vcl::Vec4i vec;
^
2 errors generated.
Desired result:
#define VCL_NAMESPACE vcl
int main() {
vcl::Vec4i vec; //compiles
Vec4i vec; //won't compile
return 0;
}
What should I change?
答案1
得分: 2
如章节 2.7 使用命名空间
中所指出的,您需要在包含任何 vcl
标头之前,直接或间接地定义 VCL_NAMESPACE
,因此将顺序更改为:
#define VCL_NAMESPACE vcl
#include "vcl/vectorclass.h" // 这行在 #define 之后
using namespace vcl; // ...现在这将起作用
如果我们查看定义 Vec4i
的 vectori128.h
文件,我们会发现该文件中的所有定义都被 #ifdef VCL_NAMESPACE
对包围:
#ifdef VCL_NAMESPACE
namespace VCL_NAMESPACE {
#endif
// 所有的定义,包括 Vec4i
#ifdef VCL_NAMESPACE
}
#endif
所以,如果在包含任何这些标头之前 不 定义 VCL_NAMESPACE
,它将 不 将它们放入您选择的命名空间中。
注意 - 您需要在包含任何 vcl
标头的 所有 文件中首先放置此 #define VCL_NAMESPACE vcl
。最简单的方式可能是告诉编译器始终定义它。g++
示例:
g++ -DVCL_NAMESPACE=vcl ...
英文:
As noted in chapter 2.7 Using a namespace
, you need to define VCL_NAMESPACE
before including any vcl
headers, directly or indirectly, so change the order to:
#define VCL_NAMESPACE vcl
#include "vcl/vectorclass.h" // this line after the #define
using namespace vcl; // ...and now this will work
If we look inside vectori128.h
which defines Vec4i
we see that all of the definitions in the file are surrounded by an #ifdef VCL_NAMESPACE
pair:
#ifdef VCL_NAMESPACE
namespace VCL_NAMESPACE {
#endif
// all the definitions, including Vec4i
#ifdef VCL_NAMESPACE
}
#endif
So, what happens is that if you don't define VCL_NAMESPACE
before including any of those headers, it will not put them in the namespace you've selected.
Note - You need to put this #define VCL_NAMESPACE vcl
first in all your files that include any vcl
header, directly or indirectly. It may be easiest to give the compiler the instruction to just define it always. g++
example:
g++ -DVCL_NAMESPACE=vcl ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论