c++ - 在 clang 中使用别名模板时,有没有办法缩短模板化类名?

标签 c++ clang c++14 clang++

这是对上一个问题 here 的跟进.基本上,下面的程序

#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>>();
}

关于c++ - 在 clang 中使用别名模板时,有没有办法缩短模板化类名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39859452/

相关文章:

c++ - Bullet Physics 刚体从表面反弹

c++ - Armadillo C++ : How to modify multiple array elements of a matrix using multiple elements from another matrix, 专门在立方体结构中?

c++ - osx : external program (qprocess) crash if parent run with open, 但如果父级直接运行则工作正常

c++ - Clang (3.6.0) 忽略包含头文件的警告

c++ - 让 SFINAE 在重载函数对象上使用 `is_callable`

c++ - 这段代码在 ISO C++ 中合法吗?

c++ - QT QMainWindow 全屏几何无框架大小

mysql - 无法在 OS X 上安装 mysql gem

c++ - 如何使用 GCC 或 Clang 构建 Objective-c++ 代码?

c++ - 值初始化 : default initialization or zero initialization?