指针的C ++模板专业化?

时间:2017-01-19 09:07:59

标签: c++ templates

我读过书< C ++模板 - 完整指南>并学习了指针的模板专业化。 (也许我误解了这本书的这一部分)

(1)这是我的简单模板:

#include <iostream>

template<typename T>
void Function(const T& a)
{
    std::cout << "Function<T>: " << a << std::endl;
}

template<typename T>
void Function<T*>(const T* a)
{
    std::cout << "Function<T*>: " << a << std::endl;
}

int main(void)
{
    Function(1);
    Function(1.2);
    Function("hello");
    Function((void*)0x25);

    return 0;
}

我使用ubuntu16.04 x64,g ++ 5.3,编译报告:

$ g++ main.cpp -o main.exe 
main.cpp:10:29: error: non-type partial specialization ‘Function<T*>’ is not allowed
 void Function<T*>(const T* a)

(2)但是这段代码是正确的:

#include <iostream>

template<typename T>
void Function(const T& a)
{
    std::cout << "Function<T>: " << a << std::endl;
}

int main(void)
{
    Function(1);
    Function(1.2);
    Function("hello");
    Function((void*)0x25);

    return 0;
}

结果显示:

$ g++ main.cpp -o main.exe
$ ./main.exe 
Function<T>: 1
Function<T>: 1.2
Function<T>: hello
Function<T>: 0x25

我的问题是:关于指针专业化的书是错的吗?或者我误解了书中这一部分的含义?或其他什么?

关于类中指针特化的更新。

(3)具有指针特化的模板类:

#include <iostream>

template<typename T>
struct Base {
    T member;

    Base(const T& a)
        : member(a)
    {
    }

    void hello()
    {
        std::cout << member << std::endl;
    }
};

template<typename T>
struct Base<T*> {
    T* member;

    Base(T* a)
        : member(a)
    {
    }

    void hello()
    {
        std::cout << member << std::endl;
    }
};

int main(void)
{
    Base<int> b1(12);
    Base<double> b2(2.4);
    Base<char*> b3("hello");
    Base<void*> b4((void*)0x25);

    b1.hello();
    b2.hello();
    b3.hello();
    b4.hello();

    return 0;
}

这个代码是正确的,有一个警告:

$ g++ main.cpp -o main.exe 
main.cpp: In function ‘int main()’:
main.cpp:37:27: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
     Base<char*> b3("hello");
                           ^
$ ./main.exe 
12
2.4
hello
0x25

(4)没有指针专门化的模板类:

#include <iostream>

template<typename T>
struct Base {
    T member;

    Base(const T& a)
        : member(a)
    {
    }

    void hello()
    {
        std::cout << member << std::endl;
    }
};

int main(void)
{
    Base<int> b1(12);
    Base<double> b2(2.4);
    Base<char*> b3("hello");
    Base<void*> b4((void*)0x25);

    b1.hello();
    b2.hello();
    b3.hello();
    b4.hello();

    return 0;
}

结果是一样的:

$ g++ main.cpp -o main.exe
main.cpp: In function ‘int main()’:
main.cpp:39:27: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
     Base<char*> b3("hello");
                           ^
$ ./main.exe 
12
2.4
hello
0x25

这是否意味着指针专精化是不必要的? 或者这个功能在不同的编译器上表现不同?

3 个答案:

答案 0 :(得分:5)

因为您已经被告知,不允许部分专业化功能模板。您可以使用std::enable_if

template <typename T, typename std::enable_if_t<!std::is_pointer<T>::value>* = 0>
void func(T val) { std::cout << val << std::endl; }

template <typename T, typename std::enable_if_t<std::is_pointer<T>::value>* = 0>
void func(T val) { func(*val); }

如果您正在寻找更简单的语法,请等待概念

答案 1 :(得分:4)

错误消息告诉您错误:

non-type partial specialization ‘Function<T*>’ is not allowed

您只能部分专门化类型(类)。您已尝试部分专门化功能。功能不是类型;你只能完全专注它们。

答案 2 :(得分:1)

两个问题:

  1. 您不能部分专门化某项功能。

  2. (void*)0x25的行为未定义。除了nullptr之外,不允许设置指向您不拥有的内存的指针,除了一个超出数组的最后一个元素,一个超过标量的地址。