const char*、const char(&)[N] 和 std::string 的函数重载
Function overloading for const char*, const char(&)[N] and std::string
我想要实现的是重载适用于字符串文字和 std::string
的函数,但会为 const char*
参数产生编译时错误。以下代码几乎可以满足我的要求:
#include <iostream>
#include <string>
void foo(const char *& str) = delete;
void foo(const std::string& str) {
std::cout << "In overload for const std::string& : " << str << std::endl;
}
template<size_t N>
void foo(const char (& str)[N]) {
std::cout << "In overload for array with " << N << " elements : " << str << std::endl;
}
int main() {
const char* ptr = "ptr to const";
const char* const c_ptr = "const ptr to const";
const char arr[] = "const array";
std::string cppStr = "cpp string";
foo("String literal");
//foo(ptr); //<- compile time error
foo(c_ptr); //<- this should produce an error
foo(arr); //<- this ideally should also produce an error
foo(cppStr);
}
我不高兴,它为 char 数组变量编译,但我认为如果我想接受字符串文字就没有办法绕过它(如果有,请告诉我)
然而,我想避免的是 std::string
重载接受 const char * const
变量。不幸的是,我不能只声明一个带有 const char * const&
参数的已删除重载,因为它也会匹配字符串文字。
知道如何让 foo(c_ptr)
产生编译时错误而不影响其他重载吗?
在该语言的现代版本中,您可以创建一些自定义类型和将创建它的 user-defined 文字,这样就可以传递 "this"_SOMEWORDS
,而不仅仅是 c 字符串文字,聊天指针或字符数组。
它不能完全满足您传递字符串文字的要求,但我认为它已经足够好了,尤其是因为它还禁止数组
为了让您删除的函数不比模板函数更匹配,这样字符串文字仍然有效,删除的函数也需要是一个模板。这似乎可以满足您的要求(尽管仍然允许使用数组):
template <typename T>
typename std::enable_if<std::is_same<std::decay_t<T>, const char*>::value>::type
foo(T&& str) = delete;
此代码执行所需的操作(数组除外 - 文字是数组,因此您不能将它们分开)
#include <cstddef>
#include <string>
template <class T>
void foo(const T* const & str) = delete;
void foo(const std::string& str);
template<std::size_t N>
void foo(const char (& str)[N]);
int main() {
const char* ptr = "ptr to const";
const char* const c_ptr = "const ptr to const";
const char arr[] = "const array";
std::string cppStr = "cpp string";
foo("String literal");
//foo(ptr); //<- compile time error
// foo(c_ptr); //<- this should produce an error
foo(arr); //<- this ideally should also produce an error
foo(cppStr);
}
我想要实现的是重载适用于字符串文字和 std::string
的函数,但会为 const char*
参数产生编译时错误。以下代码几乎可以满足我的要求:
#include <iostream>
#include <string>
void foo(const char *& str) = delete;
void foo(const std::string& str) {
std::cout << "In overload for const std::string& : " << str << std::endl;
}
template<size_t N>
void foo(const char (& str)[N]) {
std::cout << "In overload for array with " << N << " elements : " << str << std::endl;
}
int main() {
const char* ptr = "ptr to const";
const char* const c_ptr = "const ptr to const";
const char arr[] = "const array";
std::string cppStr = "cpp string";
foo("String literal");
//foo(ptr); //<- compile time error
foo(c_ptr); //<- this should produce an error
foo(arr); //<- this ideally should also produce an error
foo(cppStr);
}
我不高兴,它为 char 数组变量编译,但我认为如果我想接受字符串文字就没有办法绕过它(如果有,请告诉我)
然而,我想避免的是 std::string
重载接受 const char * const
变量。不幸的是,我不能只声明一个带有 const char * const&
参数的已删除重载,因为它也会匹配字符串文字。
知道如何让 foo(c_ptr)
产生编译时错误而不影响其他重载吗?
在该语言的现代版本中,您可以创建一些自定义类型和将创建它的 user-defined 文字,这样就可以传递 "this"_SOMEWORDS
,而不仅仅是 c 字符串文字,聊天指针或字符数组。
它不能完全满足您传递字符串文字的要求,但我认为它已经足够好了,尤其是因为它还禁止数组
为了让您删除的函数不比模板函数更匹配,这样字符串文字仍然有效,删除的函数也需要是一个模板。这似乎可以满足您的要求(尽管仍然允许使用数组):
template <typename T>
typename std::enable_if<std::is_same<std::decay_t<T>, const char*>::value>::type
foo(T&& str) = delete;
此代码执行所需的操作(数组除外 - 文字是数组,因此您不能将它们分开)
#include <cstddef>
#include <string>
template <class T>
void foo(const T* const & str) = delete;
void foo(const std::string& str);
template<std::size_t N>
void foo(const char (& str)[N]);
int main() {
const char* ptr = "ptr to const";
const char* const c_ptr = "const ptr to const";
const char arr[] = "const array";
std::string cppStr = "cpp string";
foo("String literal");
//foo(ptr); //<- compile time error
// foo(c_ptr); //<- this should produce an error
foo(arr); //<- this ideally should also produce an error
foo(cppStr);
}