相关疑难解决方法(0)

用于模板类中模板函数显式特化的C++语法?

我的代码适用于VC9(Microsoft Visual C++ 2008 SP1)但不适用于GCC 4.2(在Mac上):

struct tag {};

template< typename T >
struct C
{   
    template< typename Tag >
    void f( T );                 // declaration only

    template<>
    inline void f< tag >( T ) {} // ERROR: explicit specialization in
};                               // non-namespace scope 'structC<T>'
Run Code Online (Sandbox Code Playgroud)

我知道GCC希望我在课外移动我的显式专业,但我无法弄清楚语法.有任何想法吗?

// the following is not correct syntax, what is?
template< typename T >
template<>
inline void C< T >::f< tag >( T ) {}
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates

43
推荐指数
2
解决办法
2万
查看次数

具有常量值的C++模板专业化

给定一个模板参数的数值常量,是否有一种直接的方法来定义C++模板类的部分特化?我正在尝试为特定类型的模板组合创建特殊构造函数:

template <typename A, size_t B> class Example
{
    public:
        Example() { };

        A value[B];
};

template <typename A, 2> class Example
{
    public:
        Example(b1, b2) { value[0] = b1; value[1] = b2; };
};
Run Code Online (Sandbox Code Playgroud)

此示例将无法编译,Expected identifier before numeric constant在第二个定义中返回错误.

我已经看过这里和其他地方的一些例子,但大多数似乎都围绕着一个类型而不是一个常量.

编辑:

寻找一种编写有条件使用的构造函数的方法,其功能如下:

template <typename A, size_t B> class Example
{
    public:
        // Default constructor
        Example() { };

        // Specialized constructor for two values
        Example<A,2>(A b1, A b2) { value[0] = b1; value[1] = b2; };

        A foo() …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-specialization

33
推荐指数
3
解决办法
6万
查看次数

GCC错误:非命名空间范围的显式特化

我试图移植以下代码.我知道标准不允许在非namescape范围内进行显式特化,我应该使用重载,但我找不到在这种特殊情况下应用这种技术的方法.

class VarData
{
public:
    template < typename T > bool IsTypeOf (int index) const
    {
        return IsTypeOf_f<T>::IsTypeOf(this, index); // no error...
    }

    template <> bool IsTypeOf < int > (int index) const // error: explicit specialization in non-namespace scope 'class StateData'
    {
        return false;
    }

    template <> bool IsTypeOf < double > (int index) const // error: explicit specialization in non-namespace scope 'class StateData'
    {
        return false;
    }
};
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates

27
推荐指数
2
解决办法
2万
查看次数

为什么可以在命名空间块之外定义模板<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
查看次数

如何从结构中提取最高索引的专业化?

我正在尝试做一些模板元编程,我发现需要"提取"某种类型的某种结构的特化的最高指数.

例如,如果我有一些类型:

struct A
{
    template<unsigned int> struct D;
    template<> struct D<0> { };
};

struct B
{
    template<unsigned int> struct D;
    template<> struct D<0> { };
    template<> struct D<1> { };
};

struct C
{
    template<unsigned int> struct D;
    template<> struct D<0> { };
    template<> struct D<1> { };
    template<> struct D<2> { };
};
Run Code Online (Sandbox Code Playgroud)

那我怎么能写一个像这样的元函数:

template<class T>
struct highest_index
{
    typedef ??? type;
    // could also be:   static size_t const index = ???;
};
Run Code Online (Sandbox Code Playgroud)

给我D一个在上面的任意结构中专门的最高索引,而不 …

c++ templates sfinae template-specialization template-meta-programming

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

非命名空间作用域中的显式特化不在GCC中编译

以下代码在Clang中编译,但在GCC中不编译:

template<typename T>
struct Widget
{
    template<typename U>
    void foo(U)
    {
    }

    template<>
    void foo(int*)
    {
    }
};
Run Code Online (Sandbox Code Playgroud)

根据C++标准([temp.expl.spec],第2段):

可以在可以定义相应主模板的任何范围中声明显式特化

这是GCC中的一个错误,如果是这样,我怎样才能在它的bug追踪器中找到它?

这是GCC的输出:

prog.cc:13:14: error: explicit specialization in non-namespace scope 'struct Widget<T>'
     template<>
              ^
Run Code Online (Sandbox Code Playgroud)

我正在使用GCC HEAD 8.0.1 -std=c++2a.

c++ gcc templates bug-reporting language-lawyer

9
推荐指数
2
解决办法
1950
查看次数

C++ - 在模板类之外但在头文件中定义成员函数

我用一个成员函数定义了一个简单的类模板。它是在类外定义的,具有额外的(显式)特化,也在类外定义。全部在一个头文件中。如果您在多个翻译单元中包含此标头,则会由于 One-Definition-Rule 出现链接器错误。

// Header with a template

template <class T>
class TestClass
{
public:
    TestClass() {};
    ~TestClass() {};
    bool MemberFunction();
};

template <class T>
bool TestClass<T>::MemberFunction()
{
    return true;
}

template <>
bool TestClass<double>::MemberFunction()
{
    return true;
};
Run Code Online (Sandbox Code Playgroud)

到目前为止一切都很好。但是如果我把成员函数的定义放在类体中,链接器错误就会消失,这些函数可以在不同的翻译单元中使用。

// Header with a template

template <class T>
class TestClass
{
public:
    TestClass() {};
    ~TestClass() {};
    bool MemberFunction()
    {
        return true;
    }
};

template <>
bool TestClass<double>::MemberFunction()
{
    return true;
};
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么它会这样工作?我使用 MSVC 2012。ODR 在模板上有一些例外,我最初认为这是原因。但是类内部/外部的“Base”函数的定义在这里有所不同。

c++ templates one-definition-rule

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

如何使用模板参数包的内容填充数组?

我已经嵌套了与VS 2015一起使用的部分专用模板代码,直到我发现它不符合标准.我希望它是如此,所以我扭曲了我的代码来克服前一个问题,而且那个问题现在已经发生了变化.

使用可变参数模板和部分特化我想在给定一组固定参数的情况下在编译时填充数组.

我想要达到的目标似乎与这个答案相似,但我没有设法让它发挥作用.

考虑以下程序:

#include <cstdlib>

template <typename T, std::size_t Size>
struct Array;

template <typename T, std::size_t Size, std::size_t Iteration, typename ...Args>
struct ArrayFiller {
    inline
    static void fill(Array<T, Size>& a, const Args&... args) {
        ArrayFiller<T, Size, Iteration, Args...>::fill_recursive(a, args...);
    }

    inline
    static void fill_recursive(Array<T, Size>& a, const T& i, const Args&... args) {
        a.data[Size - Iteration - 1] = i;
        ArrayFiller<T, Size, Iteration - 1>::fill_recursive(a, args...);
    }
};

template <typename T, std::size_t …
Run Code Online (Sandbox Code Playgroud)

c++ recursion compile-time variadic-templates c++14

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

VS2010 C++成员模板函数特化错误

我有以下(最小化)代码,它在VC2005中有效,但在2010年不再有效.

template <typename TDataType>
class TSpecWrapper
  { 
  public:
    typedef typename TDataType::parent_type index_type;


  public:

    template <bool THasTriangles>
    void Spec(index_type& io_index)
      { std::cout << "False version" << std::endl; }

    template <>
    void Spec<true>(index_type& io_index)
      { std::cout << "True version" << std::endl; }
  };
Run Code Online (Sandbox Code Playgroud)

似乎当"index_type"是一个依赖类型时,我总是在特化上得到一个C2770:无效的显式模板参数错误.请注意,此代码实际上足以生成错误 - 空的main足以编译它,模板甚至不需要实例化.

如果index_type不是依赖类型,它可以正常工作.任何想法为什么在VC2010中如此,如果这实际上是标准行为或错误,并且我可以解决它?

c++ templates visual-studio-2010 specialization

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

C++模板专业化:编译错误:"不是类型"

如果我删除模板特化部分(试图打印"测试2"的部分),代码编译很好,但我希望能够有一个特殊情况,运行一个看起来干净的外部用户不同的代码路径.

#include <iostream>

using namespace std;

struct SpecialType {};

template<typename A , typename B = SpecialType>
class Test
{
public:
    class TestInner
    {
    public:
        TestInner& operator* ();
    };
};

template<typename A , typename B>
typename Test<A , B>::TestInner& Test<A , B>::TestInner::operator* ()
{
    cout << "Test 1" << endl;
    return *this;
}

// If the following is removed, everything compiles/works, but I want this alternate code path:
template<typename A>
typename Test<A , SpecialType>::TestInner& Test<A , SpecialType>::TestInner::operator* ()
{
    cout << …
Run Code Online (Sandbox Code Playgroud)

c++ templates partial-specialization specialization template-specialization

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