尝试使用C++

发布于 2025-01-23 09:31:51 字数 743 浏览 0 评论 0原文

我有一个模板函数,该功能将数组引用作为一个参数:

template<typename T, int arrSize>
void foo(T (&pArr)[arrSize]) {
    cout << "base template function" << endl;
}

我想专门为c弦乐提供此功能:

template<const char *, int arrSize>
void foo(const char * (&pArr)[arrSize]) {
    cout << "specialized template function" << endl;
}

我尝试实例化基础和专业化:

int main(int argc, const char **argv) {
    float nums[] = {0.3, 0.2, 0.11};
    const char *words[] = {"word1", "word2"};
    foo(nums);
    foo(words);
}

但是我似乎只能获得基础实例化:

./foo
base template function
base template function

我已经编译了此功能在Mac上使用clang ++和-STD = C ++ 17。

I have a templated function, which takes an array reference as a parameter:

template<typename T, int arrSize>
void foo(T (&pArr)[arrSize]) {
    cout << "base template function" << endl;
}

I want to specialize this function for C-strings:

template<const char *, int arrSize>
void foo(const char * (&pArr)[arrSize]) {
    cout << "specialized template function" << endl;
}

I try to instantiate the base and the specialization:

int main(int argc, const char **argv) {
    float nums[] = {0.3, 0.2, 0.11};
    const char *words[] = {"word1", "word2"};
    foo(nums);
    foo(words);
}

But I only seem to get the base instantiation:

./foo
base template function
base template function

I have compiled this on a Mac using clang++ with -std=c++17.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

败给现实 2025-01-30 09:31:51

尝试:

template<int arrSize>
void foo(const char * (&pArr)[arrSize]);

您正在定义一个过载,期望不可用的char const* nontype参数。

Try:

template<int arrSize>
void foo(const char * (&pArr)[arrSize]);

You were defining an overload expecting a non-deducible char const* non-type parameter.

淡淡の花香 2025-01-30 09:31:51

问题是您提供的第二个超载函数模板具有类型的非类型模板参数const char*无法为从函数参数推导。因此,要调用此超载版本,我们必须明确提供与此非类型参数相对应的模板参数。

solve 这只需删除第一个非类型模板参数,如图所示 plower

template<typename T, int arrSize>
void foo(T (&pArr)[arrSize]) {
    std::cout << "base template function" << std::endl;
}
//overload for C-strings
template< int arrSize>
void foo(const char (&pArr)[arrSize]) {
    std::cout << "single C-string overloaded version" << std::endl;
}
//overload for array of pointers to C-strings
template<std::size_t arrSize>
void foo(const char*(&pArr)[arrSize])
{
    std::cout<<" array of pointers to C-string version"<<std::endl;
}
int main(int argc, const char **argv) {
    float nums[] = {0.3, 0.2, 0.11};
    const char words[] = {"word1"};
    const char* wordPtrs[] = {"word1", "word2"};
    
    foo(nums); //calls base
    foo(words);//calls single C-string version
    foo(wordPtrs);//calls array of pointers to C-string version
}

: a href =“ https://onlinegdb.com/wnq4c2gw6” rel =“ nofollow noreferrer”> demo

另外,还请注意,功能模板不能部分专业化,但可以完全专业化,或者它们可以被超载。

The problem is that the 2nd overloaded function template that you've provided has a non-type template parameter of type const char* that cannot be deduced from the function parameter. So to call this overloaded version we have to explicitly provide the template argument corresponding to this non-type parameter.

To solve this just remove the first non-type template parameter as shown below:

template<typename T, int arrSize>
void foo(T (&pArr)[arrSize]) {
    std::cout << "base template function" << std::endl;
}
//overload for C-strings
template< int arrSize>
void foo(const char (&pArr)[arrSize]) {
    std::cout << "single C-string overloaded version" << std::endl;
}
//overload for array of pointers to C-strings
template<std::size_t arrSize>
void foo(const char*(&pArr)[arrSize])
{
    std::cout<<" array of pointers to C-string version"<<std::endl;
}
int main(int argc, const char **argv) {
    float nums[] = {0.3, 0.2, 0.11};
    const char words[] = {"word1"};
    const char* wordPtrs[] = {"word1", "word2"};
    
    foo(nums); //calls base
    foo(words);//calls single C-string version
    foo(wordPtrs);//calls array of pointers to C-string version
}

Demo

Also note that function templates cannot be partially specialized but they can be fully specialized or they can be overloaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文