相关疑难解决方法(0)

为什么允许嵌套类模板的部分特化,而完全不允许?

    template<int x> struct A {                                                                                                    
        template<int y> struct B {};.                                                                                             
        template<int y, int unused> struct C {};                                                                                  
    };                                                                                                                            

    template<int x> template<> 
    struct A<x>::B<x> {}; // error: enclosing class templates are not explicitly specialized

    template<int x> template<int unused> 
    struct A<x>::C<x, unused> {}; // ok
Run Code Online (Sandbox Code Playgroud)

那么,如果外部类也不是专用的,为什么不允许内部嵌套类(或函数)的显式特化?奇怪的是,如果我只是简单地添加一个虚拟模板参数来部分地专门化内部类,我可以解决这个问题.使事情更丑陋,更复杂,但它确实有效.

我会将完全特化视为部分特化的子集 - 特别是因为您可以通过添加伪参数将每个完整的特化表示为部分.因此,部分和完全专业化之间的消歧对我来说并没有多大意义.

不幸的是,没有人在comp.std.c ++敢于回答,所以我再次以赏金把它放在这里.

注意:我需要此功能用于一组外部类的内部类的递归模板,而内部参数的特化确实取决于外部模板参数.

c++ templates metaprogramming

42
推荐指数
3
解决办法
6677
查看次数

为什么可以在命名空间块之外定义模板<T>而不是模板<>?

这是一些不编译的代码.

namespace ns
{
    class foo
    {
        template <typename T> int bar (T *);
    };
}

template <typename T>
int ns :: foo :: bar (T*) // this is OK
{
    return 0;
}

template <>
int ns :: foo :: bar <int> (int *) // this is an error
{
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

错误是:"在'template int ns :: foo :: bar(T*)'的定义中,'template int ns :: foo :: bar(T*)'在不同命名空间[-fpermissive]中的特化"

这是一个编译的版本:

namespace ns
{
    class foo
    {
        template <typename T> int bar (T …
Run Code Online (Sandbox Code Playgroud)

c++ templates

20
推荐指数
1
解决办法
655
查看次数

Variadic递归模板mem有趣的专业化

这是我想写的代码:

template <typename T1, typename ... tail>
  class record : public record<tail...>
{
  using baseT = record<tail...>;

  T1 elem;

public:
  record(T1 first, tail... rest)
    : elem(first), baseT(rest...)
  {}

  template <int index>
  inline constexpr T1& get()
  {
        // line 83:
    return baseT::get<index-1>();
  }
  // line 85:
  template <>
  inline constexpr T1& get<0>()
  {
    return elem;
  }
};
Run Code Online (Sandbox Code Playgroud)

和我得到的编译器输出:( GCC 4.8.2 with -std=c++11)

record.hpp:85:15: error: explicit specialization in non-namespace scope 'class base::record<T1, tail>'
 template <>
       ^
record.hpp:86:33: error: template-id 'get<0>' in …
Run Code Online (Sandbox Code Playgroud)

c++ specialization variadic-templates c++11

1
推荐指数
1
解决办法
182
查看次数