根据C++'03标准2.3/1:
在进行任何其他处理之前,每个出现的以下三个字符序列之一("三字符序列")被表1中指示的单个字符替换.
Run Code Online (Sandbox Code Playgroud)---------------------------------------------------------------------------- | trigraph | replacement | trigraph | replacement | trigraph | replacement | ---------------------------------------------------------------------------- | ??= | # | ??( | [ | ??< | { | | ??/ | \ | ??) | ] | ??> | } | | ??’ | ˆ | ??! | | | ??- | ˜ | ----------------------------------------------------------------------------
在现实生活中,这意味着代码printf( "What??!\n" );
将导致打印,What|
因为??!
是一个被|
字符替换的三字符序列.
我的问题是使用三字母的目的是什么?使用三字母有什么实际优势吗?
UPD:在答案中提到一些欧洲键盘没有所有标点字符,所以非美国程序员必须在日常生活中使用三字母组合?
UPD2:Visual Studio 2010默认情况下关闭了三字母支持.
我应该用什么目的std::get_temporary_buffer
?标准说明如下:
获得一个指向存储的指针,足以存储多达n个相邻的T对象.
我认为缓冲区将在堆栈上分配,但事实并非如此.根据C++标准,这个缓冲区实际上不是暂时的.这个函数对全局函数有什么优势::operator new
,它不构造对象.我是对的,以下陈述是等同的吗?
int* x;
x = std::get_temporary_buffer<int>( 10 ).first;
x = static_cast<int*>( ::operator new( 10*sizeof(int) ) );
Run Code Online (Sandbox Code Playgroud)
这个函数只存在于语法糖吗?为什么有temporary
它的名字?
1996年7月1日Dr. Dobb's Journal提出了一个用例来实现算法:
如果没有缓冲区可以分配,或者它小于请求的缓冲区,算法仍能正常工作,它只会减慢速度.
为了创建算法模板函数,我需要知道类中的x或X(和y或Y)是模板参数.当我的函数用于MFC CPoint类或GDI + PointF类或其他类时,它可能很有用.他们都使用不同的x.我的解决方案可以简化为以下代码:
template<int> struct TT {typedef int type;};
template<class P> bool Check_x(P p, typename TT<sizeof(&P::x)>::type b = 0) { return true; }
template<class P> bool Check_x(P p, typename TT<sizeof(&P::X)>::type b = 0) { return false; }
struct P1 {int x; };
struct P2 {float X; };
// it also could be struct P3 {unknown_type X; };
int main()
{
P1 p1 = {1};
P2 p2 = {1};
Check_x(p1); // must return true
Check_x(p2); // must return false
return …
Run Code Online (Sandbox Code Playgroud) 请考虑以下C++标准ISO/IEC 14882:2003(E)引用(第5节,第4段):
除非另有说明,否则单个运算符的操作数和单个表达式的子表达式的评估顺序以及副作用发生的顺序是未指定的.53)在前一个和下一个序列点之间,标量对象应通过表达式的计算最多修改其存储值一次.此外,只能访问先前值以确定要存储的值.对于完整表达式的子表达式的每个允许排序,应满足本段的要求; 否则行为未定义.[例:
Run Code Online (Sandbox Code Playgroud)i = v[i++]; // the behavior is unspecified i = 7, i++, i++; // i becomes 9 i = ++i + 1; // the behavior is unspecified i = i + 1; // the value of i is incremented
- 末端的例子]
我很惊讶,i = ++i + 1
给出了一个未定义的值i
.有没有人知道编译器实现不能给出2
以下情况?
int i = 0;
i = ++i + 1;
std::cout << i << std::endl;
Run Code Online (Sandbox Code Playgroud)
事情是operator=
有两个args.第一个总是i
参考.在这种情况下,评估顺序无关紧要.除了C++ Standard禁忌之外,我没有看到任何问题.
请,不要不考虑这样的情况下,参数的顺序是评价非常重要.例如, …
有没有办法在Visual Studio中禁用特定代码行的编译器优化?
考虑我有以下最小代码:
#include <boost/type_traits.hpp>
template<typename ptr_t>
struct TData
{
typedef typename boost::remove_extent<ptr_t>::type value_type;
ptr_t data;
value_type & operator [] ( size_t id ) { return data[id]; }
operator ptr_t & () { return data; }
};
int main( int argc, char ** argv )
{
TData<float[100][100]> t;
t[1][1] = 5;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GNU C++给了我错误:
test.cpp: In function 'int main(int, char**)':
test.cpp:16: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than …
Run Code Online (Sandbox Code Playgroud) 我已经在我的C++项目中添加了x64配置来编译我的应用程序的64位版本.一切看起来都很好,但编译器发出以下警告:
`cl : Command line warning D9002 : ignoring unknown option '/arch:SSE2'`
Run Code Online (Sandbox Code Playgroud)
SSE2优化真的不适用于64位项目吗?
我和朋友正在讨论,我们想知道为什么这么多开源项目决定用C而不是C++.像Apache,GTK,Gnome等项目选择了C,但为什么不用C++,因为它几乎是一样的?
我们正在寻找导致这些项目(不仅是我列出的项目,而且是所有C项目)的原因,而不是使用C而不是C++.主题可以是性能,易于编程,调试,测试,概念等.
我想创建一个QApplication类型的对象,它需要主函数参数argc和argv作为输入:
QApplication app(argc, argv);
Run Code Online (Sandbox Code Playgroud)
由于我在用户定义的函数内,无法访问main函数,我想自己定义这个参数.我尝试了几种方法,但我无法获得正确的类型转换.我的最后一种方法也不起作用:
int argc = 1;
char **argv;
char arguments[1][12] = {{"cgalExample"}};
argv = arguments;
Run Code Online (Sandbox Code Playgroud)
谢谢你的任何提示.
回答完这个问题后,我试图is_complete
在Boost库中找到模板,我意识到Boost.TypeTraits中没有这样的模板.为什么Boost库中没有这样的模板?应该怎么样?
//! Check whether type complete
template<typename T>
struct is_complete
{
static const bool value = ( sizeof(T) > 0 );
};
...
// so I could use it in such a way
BOOST_STATIC_ASSERT( boost::is_complete<T>::value );
Run Code Online (Sandbox Code Playgroud)
上面的代码不正确,因为应用于sizeof
不完整类型是非法的.什么是好的解决方案?在某种程度上可以在这种情况下应用SFINAE吗?