无法在函数中声明模板化类型别名

时间:2017-10-01 07:56:19

标签: c++ templates

我正在尝试

let margins = view.layoutMarginsGuide
scrollView.leftAnchor.constraint(equalTo: margins.leftAnchor).isActive = true
scrollView.rightAnchor.constraint(equalTo: margins.rightAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: margins.bottomAnchor, constant: 0).isActive = true
scrollView.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true

scrollView.center = self.view.center // view is the base view of which scrollView is a subview of. 
subView.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: -1*scrollView.frame.width)
subView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: -1*scrollView.frame.height)

在一个函数中,但是clang会发出错误:期望的表达式。

例如

template <typename N>
using Array = std::array<std::uint8_t, N>;

会导致此错误。

这是不允许在功能中,如果是这样,为什么?感谢

2 个答案:

答案 0 :(得分:2)

是的,alias template只能在类范围或命名空间范围内声明。

  

与任何模板声明一样,别名模板只能在类范围或命名空间范围内声明。

BTW:您应该为别名模板Array声明non-type template parameter,例如

template <std::size_t N>
using Array = std::array<std::uint8_t, N>;

答案 1 :(得分:2)

  

这是不允许在功能中使用的,为什么会这样?

烨。因为C ++标准明确禁止所有模板。 [temp]/2说:

  

template-declaration 只能作为命名空间范围或类出现   范围声明。

理由可能是不要使已经脆弱的模板规范更容易受到ODR违规的影响。即使是现在,专业化的出现顺序也可能使程序形成不良或形式有些出乎意料。

问题的关键在于我们并不真正需要功能范围模板,IMO。

相关问题