简体   繁体   English

const char* 的 C++ 模板专业化问题*

[英]C++ template specialization problem with const char*

I have a template function void foo(const T&) .我有一个模板 function void foo(const T&) I need specialized implementations for [T = int] and [T = const char*] .我需要[T = int][T = const char*]的专门实现。

#include <iostream>

template<class T>
void foo(const T& arg)
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    std::cout << arg << std::endl;
}

template<>
void foo(const int& arg)
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    std::cout << arg << std::endl;
}

int main(int argc, char *argv[])
{
    int i = 42;
    const char *bar = "xyz";
    foo(i);
    foo(bar);
    return 0;
}

The specialization for [T = int] works, the output is: [T = int]的专业化工作,output 是:

void foo(const T&) [with T = int]
42
void foo(const T&) [with T = const char*]
xyz

But, it won't compile if I try to specialize for [T = const char*] like so:但是,如果我尝试像这样专门针对[T = const char*]它将无法编译:

template<>
void foo(const char*& arg)
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    std::cout << arg << std::endl;
}

17:6: error: template-id 'foo<>' for 'void foo(const char*&)' does not match any template declaration 17:6:错误:'void foo(const char*&)' 的模板 ID 'foo<>' 与任何模板声明都不匹配

What has me puzzled is that it appears to correctly deduce [T = const char*] when no specialization is present, but complains if I try to implement one.让我感到困惑的是,当没有专业化存在时,它似乎可以正确推断出[T = const char*] ,但如果我尝试实现它就会抱怨。

Any ideas where I'm going wrong?有什么想法我哪里出错了吗?

You need a second const你需要第二个 const

template<>
void foo(const char* const& arg)
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    std::cout << arg << std::endl;
}

The template requires a const reference, but const char*& is a reference to a pointer to a const char.模板需要 const 引用,但const char*&是对指向 const char 的指针的引用。

template<class T>
void foo(const T& arg)

Even better would be to use overloads instead of function template specialization , eg更好的是使用重载而不是 function 模板专业化,例如

template<class T>
void foo(const T& arg)
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    std::cout << arg << std::endl;
}

void foo(const int& arg)
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    std::cout << arg << std::endl;
}


void foo(const char*& arg)
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    std::cout << arg << std::endl;
}

In this function template declaration在这个 function 模板声明中

template<class T>
void foo(const T& arg)
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    std::cout << arg << std::endl;
}

you have a constant reference to the type T .你有一个对类型T的常量引用。 When the function is called like当 function 被称为

const char *bar = "xyz";
foo(bar);

then the deduced type T is const char * (the type of a pointer to a string literal).那么推导的类型Tconst char * (指向字符串文字的指针的类型)。 You may imagine it the following way您可以通过以下方式想象它

typedef const char *T;

So the constant reference to the type T will look like所以对类型T的常量引用看起来像

const char * const &

Here is a demonstration program.这是一个演示程序。

#include <iostream>
#include <type_traits>

template<class T>
void foo(const T& arg)
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    std::cout << arg << std::endl;
    std::cout << std::boolalpha
        << std::is_same_v<T, const char *> << '\n';
    std::cout << std::boolalpha
        << std::is_same_v<decltype( arg ), const char * const &> << '\n';
}

int main(int argc, char *argv[])
{
    const char *bar = "xyz";
    foo(bar);
    return 0;
}

The program output is程序 output 是

void foo(const T &) [T = const char *]
xyz
true
true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM