C++ 11仅接受字符串文字的函数?

And*_*zos 11 c++ linux gcc c++11

我想编写一个C++ 11函数,它只接受字符串文字作为参数:

 void f(const char* s) { static_assert(s is a string literal); ... }
Run Code Online (Sandbox Code Playgroud)

那是:

 f("foo"); // OK

 char c = ...;
 f(&c); // ERROR: Doesn't compile

 string s = ...;
 f(s.c_str()); // ERROR: Doesn't compile

 etc
Run Code Online (Sandbox Code Playgroud)

反正有没有实现这个?函数的签名对更改是开放的,添加宏或任何其他语言功能的使用也是如此.

如果这不可能,那么最接近的是什么?(用户定义的文字可以帮助吗?)

如果不是GCC 4.7/Linux中有特定于平台的方式吗?

Dir*_*ple 13

我认为你最接近的就是这个

template<int N>
void f(const char (&str)[N]){
  ...
}
Run Code Online (Sandbox Code Playgroud)

它将使用文字和数组进行编译,但不能使用指针进行编译.