在clang中使用别名模板时,是否有办法缩短模板化的类名?
Is there a way to shorten templated class names when using alias templates in clang?
这是前面一个问题的后续问题。基本上,下面的程序
#include <memory>
// Create a class parameterized on a template
template <template <typename> class XX>
struct Foo{};
// Some template with a long name
template <typename T>
struct ReallyLongFileNameThatIHateToType {
};
// Alias to shorten name
template <typename T>
using Bar = ReallyLongFileNameThatIHateToType <T>;
int main() {
std::unique_ptr<Foo<ReallyLongFileNameThatIHateToType>> foo =
std::make_unique<Foo<Bar>>();
}
在GCC下可以很好地工作,而不是clang
g++ -std=c++14 test04.cpp -o test04
clang++ -std=c++14 test04.cpp -o test04
test04.cpp:20:61: error: no viable conversion from 'unique_ptr<Foo<template
Bar>>' to
'unique_ptr<Foo<template ReallyLongFileNameThatIHateToType>>'
std::unique_ptr<Foo<ReallyLongFileNameThatIHateToType>> foo =
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/unique_ptr.h:200:17: note:
candidate constructor not viable: no known conversion from 'typename
_MakeUniq<Foo<Bar> >::__single_object' (aka 'unique_ptr<Foo<Bar> >') to
'nullptr_t' for 1st argument
constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/unique_ptr.h:205:7: note:
candidate constructor not viable: no known conversion from 'typename
_MakeUniq<Foo<Bar> >::__single_object' (aka 'unique_ptr<Foo<Bar> >') to
'std::unique_ptr<Foo<ReallyLongFileNameThatIHateToType>,
std::default_delete<Foo<ReallyLongFileNameThatIHateToType> > > &&' for 1st
argument
unique_ptr(unique_ptr&& __u) noexcept
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/unique_ptr.h:356:7: note:
candidate constructor not viable: no known conversion from 'typename
_MakeUniq<Foo<Bar> >::__single_object' (aka 'unique_ptr<Foo<Bar> >') to
'const std::unique_ptr<Foo<ReallyLongFileNameThatIHateToType>,
std::default_delete<Foo<ReallyLongFileNameThatIHateToType> > > &' for 1st
argument
unique_ptr(const unique_ptr&) = delete;
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/type_traits:1957:41: note:
candidate template ignored: disabled by 'enable_if' [with _Up = Foo<Bar>,
_Ep = std::default_delete<Foo<Bar> >]
using _Require = typename enable_if<__and_<_Cond...>::value>::type;
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/unique_ptr.h:228:2: note:
candidate template ignored: could not match 'auto_ptr' against
'unique_ptr'
unique_ptr(auto_ptr<_Up>&& __u) noexcept;
^
1 error generated.
Makefile:2: recipe for target 'all' failed
make: *** [all] Error 1
正如链接消息中指出的,这是CWG第1244期。不管怎样,有没有其他方法可以缩短clang的名字?通常,我已经在模板类上参数化了代码,但我不想一遍又一遍地键入完整的参数名。当使用gcc时,我只是创建别名模板,然后就结束了。显然,对于clang来说,这不起作用,那么是否有不同的机制来实现相同的效果呢?
作为一种解决方法,你可以让它以其他方式工作,通过将模板模板模板参数传递给一些帮助结构体来设置它与你的模板模板参数,例如(在clang++, c++14中工作):
#include <memory>
// Create a class parameterized on a template
template <template <typename> class XX>
struct Foo{};
// Some template with a long name
template <typename T>
struct ReallyLongFileNameThatIHateToType {
};
template <template <template <class> class> class FF>
struct Bar {
using type = FF<ReallyLongFileNameThatIHateToType>;
};
template <template <template <class> class> class FF>
using Bar_t = typename Bar<FF>::type;
int main() {
std::unique_ptr<Foo<ReallyLongFileNameThatIHateToType>> foo =
std::make_unique<Bar_t<Foo>>();
}
相关文章:
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 是否可以将 Clang-Tidy 与 QNX 一起使用?
- clang++ 是否以更轻松的方式处理系统标头?
- Clang 是否可以在使用可见性属性时生成导入库(不带 __declspec(dllexport))?
- 是否有 g++ 或 clang++ 调试选项来指导可变参数模板化
- 在 clang 和 gcc 中是否有视觉C++ __declspec(属性声明属性)的替代方案?
- 在这种情况下,GCC 和 clang 是否显示与 Visual Studio 相同的结果,关于语言链接?
- 是否可以使用G 或Clang -OpenMP获得矢量化报告
- clang 拒绝具有尾随 decltype 返回类型的模板调用是否正确,具体取决于其重载之一?
- 是否可以仅在Windows上使用Clang编译C ++
- GCC 或 Clang 关于函数参数在其自己的默认参数中的范围内的名称是否正确?
- 英特尔C++编译器 (19.0) 现在是否仅使用 Clang 前端(即已经放弃的 EDG)?
- clang++ 是否忽略了某些弃用警告的外部"C"?
- 如果对象在同一层次结构中,-Wreturn-std-move clang 警告是否正确
- 警告绑定到临时值的 clang 是否正确
- clang实现char8_t的方式是否存在缺陷,或者标准的某个黑暗角落是否禁止优化?
- clang,linux是否有一个选项可以在链接时更改共享库名称
- 是否可以在不使用汇编的情况下告诉clang哪些寄存器用于代码的某些部分
- clang或gcc关于这个内部类成员访问是否正确
- 是否有对 Clang 递归 ASTVisitor 的最终访问方法