鉴于我有十进制的重复表示 - 你可以在这里找到它- 我尝试用这种方式转换一个double:
explicit Decimal(double n)
{
DoubleAsQWord doubleAsQWord;
doubleAsQWord.doubleValue = n;
uint64 val = doubleAsQWord.qWord;
const uint64 topBitMask = (int64)(0x1 << 31) << 32;
//grab the 63th bit
bool isNegative = (val & topBitMask) != 0;
//bias is 1023=2^(k-1)-1, where k is 11 for double
uint32 exponent = (((uint64)(val >> 31) >> 21) & 0x7FF) - 1023;
//exclude both sign and exponent (<<12, >>12) and normalize mantissa
uint64 mantissa = ((uint64)(0x1 << 31) << 21) | …Run Code Online (Sandbox Code Playgroud) 假设我有一个类A和一个运算符<<声明如下:
// A.h
class A
{
// A stuff
};
std::ostream& operator<<(std::ostream& os, const A& a);
Run Code Online (Sandbox Code Playgroud)
在其他地方我用A记录器:
LoggerPtr logger(LogManager::getLogger("ThisObject"));
A a;
LOG4CXX_INFO(logger, "A: " << a);
Run Code Online (Sandbox Code Playgroud)
编译器抱怨:二进制'<<':找不到运算符,它采用类型'const A'的右手操作数(或者没有可接受的转换)D:\ dev\cpp\lib\apache-log4cxx\log4cxx\include\log4cxx\helpers\messagebuffer.h 190
这个错误带我去声明operator<<:
// messagebuffer.h
template<class V>
std::basic_ostream<char>& operator<<(CharMessageBuffer& os, const V& val) {
return ((std::basic_ostream<char>&) os) << val;
}
Run Code Online (Sandbox Code Playgroud)
LOG4XX_INFO 宏扩展为:
#define LOG4CXX_INFO(logger, message) { \
if (logger->isInfoEnabled()) {\
::log4cxx::helpers::MessageBuffer oss_; \
logger->forcedLog(::log4cxx::Level::getInfo(), oss_.str(oss_ << message), LOG4CXX_LOCATION); }}
Run Code Online (Sandbox Code Playgroud)
MessageBuffer "定义"此运算符:
// messagebuffer.h
template<class V>
std::ostream& operator<<(MessageBuffer& os, const V& …Run Code Online (Sandbox Code Playgroud) 我定义了一个像这样的方法:
template <class ArgT>
void foo(ArgT arg, ::boost::function< void(ArgT) > func)
{
func(arg);
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用 - 例如 - :
foo(2, [](int i) -> void { cout << i << endl; });
Run Code Online (Sandbox Code Playgroud)
为什么编译器不能推断出类型,因为它肯定是一个int?
我得到'void foo(ArgT,boost::function<void(ArgT)>)' : could not deduce template argument for 'boost::function<void(ArgT)>' from 'anonymous-namespace'::<lambda0>'.
我最近一直试图使用https://github.com/ybainier/Hypodermic在C++上进行依赖注入.但是,我无法找到它是否支持AutoFac的OnActivating事件:
builder.RegisterInstance(instance).OnActivating(MyLambdaHere)
Run Code Online (Sandbox Code Playgroud)
Hypodermic有没有办法模仿这个功能?
考虑这些对象:
struct A
{
virtual void foo() = 0;
};
struct B
{
void foo() { /* neat implementation */ }
};
Run Code Online (Sandbox Code Playgroud)
我想知道为什么 - 编译器 - 明智 - 以下对象被认为是抽象的:
struct C : B, A
{
using B::foo; // I tried my best to make the compiler happy
};
Run Code Online (Sandbox Code Playgroud)
编译器不允许我这样做:
A* a = new C;
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2010说:
'C':由于以下成员无法实例化抽象类:'void A :: foo(void)':是抽象的:请参阅'A :: foo'的声明
g ++ 4.6.3说:
无法分配抽象类型为"C"的对象,因为以下虚函数在'C'中是纯的:virtual void A :: foo()
我想这在C#中是允许的,但我不知道所涉及的机制主义 - 我只是好奇.
说我有这个号码:
// bmp = boost::multiprecision
bmp::cpp_dec_float n("123456789.1234567891011121314");
Run Code Online (Sandbox Code Playgroud)
它的后端数据是:
[0] 1 unsigned int
[1] 23456789 unsigned int
[2] 12345678 unsigned int
[3] 91011121 unsigned int
[4] 31400000 unsigned int
... 0
[15] 0 unsigned int
Run Code Online (Sandbox Code Playgroud)
这正是我想得到的; 不幸的是,我找不到一种方法来获取我的号码的两个部分bmp::int128_t- 例如 - ,或者我的号码的基础数据.
也就是说,我喜欢这样的东西存在:
bmp::int128_t integerPart;
bmp::int128_t floatPart;
n.getParts(integerPart, floatPart);
Run Code Online (Sandbox Code Playgroud)
要么
auto&& data = n.data(); // which is actually private when using `cpp_dec_float`.
Run Code Online (Sandbox Code Playgroud)
无论如何,有人知道如何做我想要实现的目标吗?
为了记录,为了互操作性,我需要将此十进制数表示为C#十进制数.
关于这个问题是否存在32位变量无法正确对齐并提供答案的情况,我可以假设在Windows平台下工作时我可以交换地址而没有任何副作用吗?
例如:
struct Foo
{
// whatever Foo can hold
};
struct Bar
{
void buildFoo()
{
auto tmp = new Foo;
// do some stuff on tmp, or not
foo = tmp;
}
Foo* foo;
};
Run Code Online (Sandbox Code Playgroud)
现在,让一些线程foo通过实例Bar和其他线程调用会产生什么后果Bar::buildFoo()?
说我有这些类型:
template
<
class T,
template <class> class Storage
>
struct AbstractFactoryUnit
{
virtual ~AbstractFactoryUnit() {}
virtual typename Storage< T >::StoredType doCreate(Storage< T >) = 0;
};
Run Code Online (Sandbox Code Playgroud)
和
template
<
class TypeSequence,
template <class> class ProductStorage,
template <class, template <class> class> class Unit = AbstractFactoryUnit
>
struct AbstractFactory
: boost::mpl::inherit_linearly
<
TypeSequence,
boost::mpl::inherit
<
boost::mpl::_1,
Unit< boost::mpl::_2, ProductStorage >
>
>::type
{
typedef TypeSequence Products;
template <class T>
auto create() -> typename ProductStorage< T >::StoredType
{
Unit< T, ProductStorage >& …Run Code Online (Sandbox Code Playgroud) 我有很多类暴露一个名为的内部类型Binding.例如,其中一个可能是:
struct Message
{
struct Binding
{
};
};
Run Code Online (Sandbox Code Playgroud)
我调用这样的函数apply:
apply< Message >([](Message::Binding& x)
{
// setup binding fields
});
Run Code Online (Sandbox Code Playgroud)
因为我写了
template <class TMessage, class TBindingExpression>
void apply(const TBindingExpression& expr)
{
typedef typename TMessage::Binding BindingType;
BindingType binding;
expr(binding);
apply(MessageUtil::typeId< TMessage >(), binding);
}
Run Code Online (Sandbox Code Playgroud)
因为Message我调用的方式有点多余,所以我apply想让编译器推断出来Message以便我可以编写
apply([](Message::Binding x)
{
//...
});
Run Code Online (Sandbox Code Playgroud)
到目前为止,我被困在这里:
template <class TBindingExpression>
void apply(const TBindingExpression& expr)
{
// I get the type of the argument which is Message::Binding in …Run Code Online (Sandbox Code Playgroud) 说我有一个模板化的动作
template <class ArgT>
struct Action
{
Action(::boost::function< void(ArgT) > func)
: func_(func)
{
}
void operator()(ArgT arg)
{
func_(arg);
}
private:
::boost::function< void(ArgT) > func_;
};
Run Code Online (Sandbox Code Playgroud)
我像这样使用Action:
class XCallbackInvoker : public CallbackInvoker< X >
{
public:
XCallbackInvoker (Action< int > callback)
: CallbackInvoker< X >(
Action< ::boost::shared_ptr< X > >(
[&callback](::boost::shared_ptr< X > x) -> void
{
Action< int > callbackCopy = callback;
callbackCopy(x->errorCode());
}))
{
}
};
Run Code Online (Sandbox Code Playgroud)
编辑:添加了CallbackInvoker
template <class T>
class CallbackInvoker : public ICallbackInvoker
{
public: …Run Code Online (Sandbox Code Playgroud)