我在嵌套命名空间中有一个模板类的前向声明
namespace n1
{
namespace n2
{
template <typename T, typename S>
struct A;
}
using n2::A;
}
接着是一个定义,实际上它在不同的文件中,中间有一些东西:
struct X { };
namespace n1
{
namespace n2
{
template <typename T, typename S = X>
struct A { };
}
using n2::A;
}
那么以下总是可以的:
n1::n2::A <int> a;
但是这个捷径
n1::A <int> a;
在 clang 中给出编译错误
error: too few template arguments for class template 'A'
除非我删除前向声明; g++ 两者都接受。
clang 似乎保留在第一个不包含默认模板参数的声明中(我不能包含它,因为我还没有定义 X
)。
如果我使用单个命名空间没有问题(但这不是解决方案)。
我做错了什么,或者哪个编译器是正确的?快捷方式如何与前向声明和嵌套命名空间一起使用?我需要所有这些。
前向声明 X
+ S 的默认参数当然可以,但是太乏味了(实际上有几十个,整个文件结构会改变)。
最佳答案
template<class T> using myname = some::templatething<T>;
然后你可以使用我的名字
在你的情况下贴一个template<class T,class S=X> using A = n2::A<T,S>;
在你的n1
刚刚写了一个与此相关的答案的精华 Symbol not found when using template defined in a library顺便说一句,读一读。
好吧,它没有被勾选,所以我会再帮助一些!
不会编译
#include <iostream>
namespace n1 {
namespace n2 {
template<class U,class V> struct A;
}
template<class U,class V> using A = n2::A<U,V>;
}
static n1::A<int,int>* test;
struct X {};
namespace n1 {
namespace n2 {
template<class U,class V> struct A {};
}
template<class U,class V=X> using A = n2::A<U,V>;
}
static n1::A<int> test2;
int main(int,char**) {
return 0;
}
为什么? C++的“先声明规则”
这是编译器的输出:
make all
if ! g++ -Isrc -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings -MM src/main.cpp >> build/main.o.d ; then rm build/main.o.d ; exit 1 ; fi
g++ -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings -Isrc -c src/main.cpp -o build/main.o
src/main.cpp:26:17: error: wrong number of template arguments (1, should be 2)
static n1::A<int> test2;
^
src/main.cpp:13:47: error: provided for ‘template<class U, class V> using A = n1::n2::A<U, V>’
template<class U,class V> using A = n2::A<U,V>;
^
src/main.cpp:26:24: error: invalid type in declaration before ‘;’ token
static n1::A<int> test2;
^
src/main.cpp:16:24: warning: ‘test’ defined but not used [-Wunused-variable]
static n1::A<int,int>* test;
^
src/main.cpp:26:19: warning: ‘test2’ defined but not used [-Wunused-variable]
static n1::A<int> test2;
^
make: *** [build/main.o] Error
好吧,它提示未使用的变量,很公平,但请注意它是对第 13 行的引用,那是因为它使用了第一个定义,默认模板参数非常原始,我现在不能向您引用规范。 https://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fdefault_args_for_templ_params.htm
虽然这可能会提供一些见解。
无论如何请注意:
这个编译
#include <iostream>
namespace n1 {
namespace n2 {
template<class U,class V> struct A;
}
template<class U,class V> using A = n2::A<U,V>;
}
static n1::A<int,int>* test;
struct X {};
namespace n1 {
namespace n2 {
template<class U,class V> struct A {};
}
template<class U,class V=X> using B = n2::A<U,V>;
}
static n1::B<int> test2;
int main(int,char**) {
return 0;
}
因为B没有事先定义。
构建输出
make all
if ! g++ -Isrc -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings -MM src/main.cpp >> build/main.o.d ; then rm build/main.o.d ; exit 1 ; fi
g++ -Wall -Wextra -O3 -std=c++11 -g -gdwarf-2 -Wno-write-strings -Isrc -c src/main.cpp -o build/main.o
src/main.cpp:16:24: warning: ‘test’ defined but not used [-Wunused-variable]
static n1::A<int,int>* test;
^
src/main.cpp:26:19: warning: ‘test2’ defined but not used [-Wunused-variable]
static n1::B<int> test2;
^
g++ build/main.o -o a.out
看,很好:)
请记住,对于前向声明,您只能使用 *s 和 &s(因为它们的大小已知,实际上是固定大小)- oh 和 &&s
所以您需要立即获取默认值!
关于c++ - 嵌套命名空间 : where should default template arguments go? 中模板类的前向声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18543182/