我需要编写函数double_to_int(double val, int *err),在可能的情况下,它会将double val转换为整数; 否则报告错误(NAN/INFs/OUT_OF_RANGE).
所以伪代码实现看起来像:
if isnan(val):
err = ERR_NAN
return 0
if val < MAX_INT:
err = ERR_MINUS_INF
return MIN_INT
if ...
return (int)val
Run Code Online (Sandbox Code Playgroud)
在SO上至少有两个类似的问题:在这个答案中它以足够干净的方式解决了,虽然它是C++解决方案 - 在C中我们没有signed int的可移植数字.在这个答案中,它解释了为什么我们不能只检查(val > INT_MAX || val < INT_MIN).
因此,我看到的唯一可能的清洁方式是使用浮点环境,但它被称为实现定义的功能.
所以我的问题是:有没有办法以double_to_int跨平台的方式实现功能(仅基于C标准,甚至不考虑支持IEEE-754的目标平台).
来自temp.local:
在出现在类模板定义之外的类模板成员的定义中,类模板成员的名称隐藏任何封闭类模板的模板参数的名称(但不是模板参数的名称)成员,如果成员是类或函数模板).[例如:
Run Code Online (Sandbox Code Playgroud)template<class T> struct A { struct B { /* ... */ }; typedef void C; void f(); template<class U> void g(U); }; template<class B> void A<B>::f() { B b; // A's B, not the template parameter } template<class B> template<class C> void A<B>::g(C) { B b; // A's B, not the template parameter C c; // the template parameter C, not A's C }- 结束例子]
问题是,我尝试过的每个编译器(g ++,vc,icc,clang)都将C A<B>::g(C)作为A的成员名称处理,并且不编译该示例.
这是一个常见的错误吗?
我与一个程序员进行了讨论,其主要观点是foo可以传递以下断言,具体取决于编译器.
#include <cassert>
const int i = 0 ;
void foo ( const int& i )
{
assert( &::i == &i ) ;
}
int main ( )
{
foo( i ) ;
}
Run Code Online (Sandbox Code Playgroud)
他告诉我,可以评估(&i)表达式以解决某些临时对象的问题.既然我有疑惑,我就在这里.对函数的引用如何传递给函数,如果在函数中我可以检查并做任何我想要的事情和参数的地址和预期的语义必须保留.例如
#include <initializer_list>
const int i = 0 ;
bool func ( const int & i )
{
return &::i == & i ;
}
int main ()
{
const int i = 0 ;
for ( const int * each : { &::i …Run Code Online (Sandbox Code Playgroud) g++、icc 和 clang 接受以下代码:
namespace ns { struct A ; }
namespace ns1 // note : not enclosing namespace
{
using namespace ns ;
struct A { } ;
}
Run Code Online (Sandbox Code Playgroud)
由于标准,这是一个有效的代码。?
[命名空间。qual ]#7: ... 然而,在这样的命名空间成员声明中,嵌套名称说明符可能依赖 using-directives 隐式提供嵌套名称说明符的初始部分。
如相关答案所述,
您可以省略nested-name-specifier 的初始部分,但不能省略任何中间部分。
(从我知道它可以/必须在封闭的命名空间中完成,我不记得了。)
来自[dcl.link]#2:
外部字符串文字声明
该段未在函数范围内指定用于声明的任何特殊情况,但以下内容也未编译:
void foo () { extern "C" int boo () ; }
Run Code Online (Sandbox Code Playgroud)
从g ++输出:
错误:字符串常量之前的预期unqualified-id
那是g ++的bug还是我错过了什么?
Rust 是否可以保护我免受迭代器失效的影响,还是我很幸运realloc?为返回的迭代器提供了哪些保证&'a Vec<T>?
fn main() {
let mut v = vec![0; 2];
println!("capacity: {}", v.capacity());
{
let v_ref = &mut v;
for _each in v_ref.clone() {
for _ in 0..101 {
(*v_ref).push(1); // ?
}
}
}
println!("capacity: {}", v.capacity());
}
Run Code Online (Sandbox Code Playgroud) c++ ×5
rust ×2
standards ×2
c ×1
class ×1
const ×1
definition ×1
extern ×1
invalidation ×1
iterator ×1
linkage ×1
name-lookup ×1
namespaces ×1
nullptr ×1
self ×1
templates ×1
vector ×1