这是我的绑定(缩短,Command-Property也绑定)
<MenuItem Header="Key" CommandParameter="{Binding StringFormat='Key: {0}', Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
Run Code Online (Sandbox Code Playgroud)
ContectMenu的PlacementTarget的Tag-Property是一个类似String的属性
"Short.Plural"
Run Code Online (Sandbox Code Playgroud)
我期望在Command-Handler中收到的是:
Key: Short.Plural
Run Code Online (Sandbox Code Playgroud)
但我真正得到的是:
Short.Plural
Run Code Online (Sandbox Code Playgroud) 我使用printf()语句创建了以下输出:
printf("She said time flies like an arrow, but fruit flies like a banana.");
Run Code Online (Sandbox Code Playgroud)
但是我想把实际的引号用双引号,所以输出是
她说"时间过得像箭一样,但果实像香蕉一样苍蝇".
不干扰用于在printf()语句中包装字符串文字的双引号.
我怎样才能做到这一点?
来自维基百科:
为了在C++编译器中增强对Unicode的支持,char类型的定义已被修改为至少为存储UTF-8的8位编码所需的大小.
我想知道这对于编写便携式应用程序究竟意味着什么.写这个有什么区别吗?
const char[] str = "Test String";
Run Code Online (Sandbox Code Playgroud)
或这个?
const char[] str = u8"Test String";
Run Code Online (Sandbox Code Playgroud)
是否有任何理由不在代码中的每个字符串文字中使用后者?
当TestString中有非ASCII字符时会发生什么?
Haskell 2010是否保证在编译时连接字符串文字?
如果我有
"This is a " ++
"very long String that " ++
"spans several lines"
Run Code Online (Sandbox Code Playgroud)
编译器会将其视为
"This is a very long String that spans several lines"
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我想保持我的源行长度不超过80个字符,但我不想引入运行时效率低下.
是否有可能创建一个只接受字符串文字的构造函数(或函数签名),而不是例如char const *?
是否有可能有两个可以区分字符串文字的重载char const *?
C++ 0x会允许使用自定义后缀 - 但我正在寻找"早期"解决方案.
基本原理:避免在作为字符串文字给出时不会被修改的字符串的堆副本.
这些字符串直接转到API,期望const char *没有任何处理.大多数调用确实使用不需要额外处理的文字,只在少数情况下构建它们.我正在寻找保留原生呼叫行为的可能性.
注意: - 因为它出现在答案中:有问题的代码根本不使用std::string,但一个很好的例子是:
class foo
{
std::string m_str;
char const * m_cstr;
public:
foo(<string literal> s) : m_cstr(p) {}
foo(char const * s) : m_str(s) { m_cstr = s.c_str(); }
foo(std::string const & s) : m_str(s) { m_cstr = s.c_str(); }
operator char const *() const { return m_cstr; }
}
Run Code Online (Sandbox Code Playgroud)
结果:
(1)不能做到.
(2)我意识到我甚至不是在寻找一个文字,而是一个编译时常量(即"任何不需要复制的东西").
我可能会使用以下模式: …
#include <iostream>
#include <typeinfo>
int main()
{
const char a[] = "hello world";
const char * p = "hello world";
auto x = "hello world";
if (typeid(x) == typeid(a))
std::cout << "It's an array!\n";
else if (typeid(x) == typeid(p))
std::cout << "It's a pointer!\n"; // this is printed
else
std::cout << "It's Superman!\n";
}
Run Code Online (Sandbox Code Playgroud)
x当字符串文字实际上是数组时,为什么推断为指针?
窄字符串文字的类型为"数组n
const char"[2.14.5字符串文字[lex.string]§8]
假设我们有一个带有非类型参数的模板函数,const char *如下所示:
template <const char * MESSAGE> void print() {
std::cout << MESSAGE << '\n';
}
Run Code Online (Sandbox Code Playgroud)
使用此模板不会像日志那样成为MESSAGE可以在编译时推断出来的问题,因此以下用法是合法的:
namespace {
char namespace_message[] = "Anonymous Namespace Message";
constexpr char namespace_constexpr_message[] = "Anonymous Namespace Constexpr Message";
}
char message[] = "Message";
constexpr char constexpr_message[] = "Constexpr Message";
int main()
{
print<namespace_message>();
print<namespace_constexpr_message>();
print<message>();
print<constexpr_message>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但下面的那些不是(见这里):
namespace {
const char namespace_const_message[] = "Anonymous Namespace Const Message";
}
const char const_message[] = "Const Message";
int …Run Code Online (Sandbox Code Playgroud) 我提到http://en.cppreference.com/w/cpp/language/typeid来编写代码,它为不同的类型做了不同的事情.
代码如下,注释中给出了解释.
#include <iostream>
#include <typeinfo>
using namespace std;
template <typename T>
void test_template(const T &t)
{
if (typeid(t) == typeid(double))
cout <<"double\n";
if (typeid(t) == typeid(string))
cout <<"string\n";
if (typeid(t) == typeid(int))
cout <<"int\n";
}
int main()
{
auto a = -1;
string str = "ok";
test_template(a); // Prints int
test_template("Helloworld"); // Does not print string
test_template(str); // Prints string
test_template(10.00); // Prints double
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么test_template(str)打印"字符串"而test_template("Helloworld")不是?
顺便说一句,我的g ++版本是g ++(Ubuntu 5.4.0-6ubuntu1~16.04.4)5.4.0 20160609.
这些查询之间的含义和区别是什么?
SELECT U'String' FROM dual;
Run Code Online (Sandbox Code Playgroud)
和
SELECT N'String' FROM dual;
Run Code Online (Sandbox Code Playgroud) 假设我需要调用一个函数foo,该函数std::string从我的代码中的很多地方获取const 引用:
int foo(const std::string&);
..
foo("bar");
..
foo("baz");
Run Code Online (Sandbox Code Playgroud)
使用像这样的字符串文字调用函数将创建临时std::string对象,每次复制文字.
除非我弄错了,否则编译器不会通过为std::string每个文字创建一个可以重复用于后续调用的静态对象来优化它.我知道g ++有高级字符串池机制,但我不认为它扩展到std::string对象本身.
我自己可以做这个"优化",这使代码的可读性降低:
static std::string bar_string("bar");
foo(bar_string);
..
static std::string baz_string("baz");
foo(baz_string);
Run Code Online (Sandbox Code Playgroud)
使用Callgrind,我可以确认这确实加快了我的程序.
我以为我会尝试为此制作一个宏,但我不知道是否可能.我想要的是:
foo(STATIC_STRING("bar"));
..
foo(STATIC_STRING("baz"));
Run Code Online (Sandbox Code Playgroud)
我尝试使用文字作为模板参数创建模板,但事实证明这是不可能的.由于代码块中的函数定义是不可能的,我完全没有想法.
有没有一种优雅的方式来做到这一点,还是我不得不求助于那种不太可读的解决方案?
string-literals ×10
c++ ×7
string ×4
c++11 ×3
literals ×2
auto ×1
binding ×1
c ×1
compile-time ×1
constructor ×1
escaping ×1
haskell ×1
macros ×1
oracle ×1
overloading ×1
sql ×1
stdstring ×1
templates ×1
utf-8 ×1
wpf ×1