有没有办法在一个或多个嵌套循环中立即从函数返回?
以下是一些说明问题的示例代码:
; Grid data structure
; -------------------
(defstruct grid :width :height)
(defn create-grid [w h initial-value]
(struct-map grid
:width w
:height h
:data (ref (vec (repeat (* w h) initial-value)))))
(defn create-grid-with-data [w h gdata]
(struct-map grid
:width w
:height h
:data (ref gdata)))
(defn get-grid [g x y]
(let [gdata (g :data)
idx (+ x (* (g :width) y)) ]
(gdata idx)))
(defn set-grid [g x y value]
(let [data (deref (g :data))
idx (+ x (* (g …Run Code Online (Sandbox Code Playgroud) 工作中的代码库包含一些看起来大致如下的代码:
#define DATA_LENGTH 64
u_int32 SmartKey::SerialNumber()
{
unsigned char data[DATA_LENGTH];
// ... initialized data buffer
return *(u_int32*)data;
}
Run Code Online (Sandbox Code Playgroud)
此代码可以正常工作,但GCC会发出以下警告:
warning: dereferencing pointer ‘serialNumber’ does break strict-aliasing rules
Run Code Online (Sandbox Code Playgroud)
有人可以解释这个警告吗?这个代码有潜在危险吗?怎么改进?
更新
感谢James McNellis的回答,我提出了以下实用功能:
template<class T, class Data>
T BinaryCast(const Data & inData)
{
T ret;
std::copy(&inData[0], &inData[0] + sizeof(ret), reinterpret_cast<char*>(&ret));
return ret;
}
u_int32 SmartKey::SerialNumber()
{
unsigned char data[DATA_LENGTH];
// ... initialized data buffer
return BinaryCast<u_int32>(data);
}
Run Code Online (Sandbox Code Playgroud)
随意提出改进建议!
我目前正在代码库中工作,其中IPv4地址表示为指针u_int8.等于运算符的实现方式如下:
bool Ipv4Address::operator==(const u_int8 * inAddress) const
{
return (*(u_int32*) this->myBytes == *(u_int32*) inAddress);
}
Run Code Online (Sandbox Code Playgroud)
这可能是禁食的解决方案,但它会导致GCC编译器警告:
ipv4address.cpp:65: warning: dereferencing type-punned pointer will break strict-aliasing rules
Run Code Online (Sandbox Code Playgroud)
如何在不破坏严格别名规则且不丢失性能点的情况下正确地重写比较?
我考虑使用其中一个memcmp或这个宏:
#define IS_EQUAL(a, b) \
(a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
Run Code Online (Sandbox Code Playgroud)
我认为宏是最快的解决方案.
您有什么推荐的吗?
更新
我刚刚阅读文章压缩memcmp用法的性能,解释了编译器(Visual Studio,但也许还有GCC)如何优化!memcmp(..)调用.
我似乎无法找到一种方法来启动包含contrib库的Clojure REPL.如果我正确理解文档,那么这个命令应该这样做:
C:\clojure-1.1.0>"%ProgramFiles%\Java\jre6\bin\java.exe" -cp clojure.jar:clojure
-contrib.jar clojure.main
Exception in thread "main" java.lang.NoClassDefFoundError: clojure/main
Caused by: java.lang.ClassNotFoundException: clojure.main
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: clojure.main. Program will exit.
Run Code Online (Sandbox Code Playgroud)
但正如你所看到的,它失败了.我确实将clojure-contrib.jar复制到了该C:\clojure-1.1.0文件夹中.
有人可以帮助我做对吗?
更新
感谢Micha?的帖子我注意到我的错误是使用冒号,我不得不使用分号.这有效:
C:\clojure-1.1.0>"%ProgramFiles%\Java\jre6\bin\java.exe" -cp clojure.jar;clojure-contrib.jar clojure.main
Clojure 1.1.0
user=>
Run Code Online (Sandbox Code Playgroud) 如果父窗口设置了 WS_CLIPCHILDREN 样式,则组框无法正确呈现。我当前的解决方法是简单地从父级中删除该标志。然而,这会导致在调整窗口大小时出现极度闪烁。
有更好的解决方法吗?
多布斯博士的文章《重新调整大小的对话框》解决了我遇到的完全相同的问题。所提供的解决方案减少了闪烁,但并没有消除它。我将使用这段代码。但是,如果可能有更好的解决方案,请随时发布!
我最近发现使用以下帮助器类时可以简化函数指针语法:
template<typename Sig>
struct Fun {
typedef Sig* Ptr;
};
Run Code Online (Sandbox Code Playgroud)
它允许我指向void()如下:
typedef Fun<void()>::Ptr fun_ptr;
fun_ptr f = foo;
Run Code Online (Sandbox Code Playgroud)
我想创建一个类似的实用程序来创建一个typedef到成员函数指针.它将允许以下语法:
struct Foo {
void bar() {}
};
typedef MemFun<Foo, void()>::Ptr bar_type;
bar_type b = &Foo::bar;
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚typedef语法:
template<class T, typename Sig>
struct MemFun {
// How to use T and Sig to create member function typedef?
};
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
如果我需要执行std :: function调用,则使用调试器单步执行功能对象可能会造成混乱。
是否可以跳过这些帧?
的实施std::min对cppreference,并在原有的STL是这样的:
return (b < a) ? b : a;
Run Code Online (Sandbox Code Playgroud)
但我认为这更具可读性:
return (a < b) ? a : b;
Run Code Online (Sandbox Code Playgroud)
这让我想知道:这两个实现是否相同?是否有一个特殊原因可以实现它?
根据cachegrind,这个校验和计算例程是整个应用程序中指令缓存加载和指令缓存未命中的最大因素之一:
#include <stdint.h>
namespace {
uint32_t OnesComplementSum(const uint16_t * b16, int len) {
uint32_t sum = 0;
uint32_t a = 0;
uint32_t b = 0;
uint32_t c = 0;
uint32_t d = 0;
// helper for the loop unrolling
auto run8 = [&] {
a += b16[0];
b += b16[1];
c += b16[2];
d += b16[3];
b16 += 4;
};
for (;;) {
if (len > 32) {
run8();
run8();
run8();
run8();
len -= 32;
continue;
}
if (len > …Run Code Online (Sandbox Code Playgroud) 在这个程序中,我使用typeid来检查对象的派生类型:
#include <cstdint>
#include <memory>
#include <cassert>
#include <string>
#include <typeinfo>
struct Wrap
{
explicit Wrap(int64_t id) : mImpl(new Impl<int64_t>(id)) {}
explicit Wrap(std::string id) : mImpl(new Impl<std::string>(std::move(id))) {}
bool isInt64() const
{
const ImplBase& impl = *mImpl;
return (&typeid(impl) == &typeid(const Impl<int64_t>));
}
bool isString() const
{
const ImplBase& impl = *mImpl;
return &typeid(impl) == &typeid(const Impl<std::string>);
}
private:
struct ImplBase
{
virtual ~ImplBase() {}
};
template<typename T>
struct Impl : ImplBase
{
Impl(T value) :
mValue(std::move(value))
{
}
T …Run Code Online (Sandbox Code Playgroud)