Skip to content

如何优雅的调用 template<std::size_t N> void func(const char(&str)[N]) 模板函数? #321

Answered by Mq-b
Matrix-A asked this question in Q&A
Discussion options

You must be logged in to vote

数组类型隐式转换为指针的优先级比常规隐式转换的高的多:

#include <iostream>

auto f(const auto&){
    puts("1");
}
auto f(const int&){
    puts("2");
}

int main(){
    double a{};
    f(a);
}

这段代码必然输出 1https://godbolt.org/z/eME1PPY3n

数组的事没什么办法。

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Mq-b
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants