我想生产一个32位,64位和128位的morton密钥,具有最佳代码!解决方案是什么?
我有以下简单的代码:
#include <cmath>
struct init_sin
{
typedef double type;
static constexpr type value(int index) {
return 3*std::pow(std::sin(index * 2.0 * 3.1415 / 20.0),1.999);
}
};
int main(){
static double VALUE = init_sin::value(10);
double VALUE_NONSTAT = 3*std::pow(std::sin(10 * 2.0 * 3.1415 / 20.0),1.999);
return int(VALUE_NONSTAT);
}
Run Code Online (Sandbox Code Playgroud)
我想知道汇编代码的含义是什么.这里是程序集的链接:http://pastebin.com/211AfSYh
我认为这VALUE是编译时编译的,并直接作为汇编程序代码中的值,如果我没有弄错的话应该在这一行中:
33 .size main, .-main
34 .data
35 .align 8
36 .type _ZZ4mainE5VALUE, @object
GAS LISTING /tmp/ccbPDNK8.s page 2
37 .size _ZZ4mainE5VALUE, 8
38 _ZZ4mainE5VALUE:
39 0000 15143B78 .long …Run Code Online (Sandbox Code Playgroud) 我有一个奇怪的gcc 4.7问题启用了c ++ 11:
当我想编译这个:
constexpr unsigned int getDim(const int e){
return (e==1)? A::Set::Dimension :
(
(e==2)? B::Set::Dimension :
(
(e==3)? C::Set::Dimension :
(
+D::Set::Dimension
)
)
);
}
Run Code Online (Sandbox Code Playgroud)
其中对于每个结构体A,B,C,D的类型定义为Set定义所述相关联的集合具有一个int Dimension,例如
struct SetOne{
static const int Dimension = 1;
}
struct A{
typedef SetOne Set;
}
Run Code Online (Sandbox Code Playgroud)
如果我不使用链接器的unary +infront D::Set::Dimension就会抱怨对SetOne :: Dimension的未定义引用.
这是否与以下问题相同:对静态类成员的未定义引用
我不能给MWE,因为一个.cpp文件的简单示例消失了问题.?(但是A,B,C,D的所有定义都在一个头文件中)
有没有人知道这里可能出现什么问题?这是unintuitiv :-)
观察2:
如果一个替换:+D::Set::Dimension0,它编译好,但为什么黑客其他语句A::Set::Dimension不会引起相同的链接错误?
对于看到这个问题的人:看看答案并考虑使用:cdecl
为什么下面的代码会给出编译器错误:
prog.cpp: In function ‘int main()’:
prog.cpp:23:4: error: request for member ‘size’ in ‘a’, which is of non-class type ‘RangeVec<int>(RangeVec<int>)’
a.size();
^
Run Code Online (Sandbox Code Playgroud)
我不明白这段代码有什么问题?
#include <iostream>
#include <vector>
template<typename Type>
class RangeVec: public std::vector<Type> {
public:
RangeVec( const RangeVec & v): std::vector<Type>(v){}
RangeVec( const RangeVec && v): std::vector<Type>(std::move(v)) {}
RangeVec( const std::vector<Type> &v)
: std::vector<Type>(v)
{
//std::sort(std::vector<Type>::begin(),std::vector<Type>::end());
}
};
int main(){
std::vector<int> v;
RangeVec<int> b(v);
RangeVec<int> a( RangeVec<int>(b) );
a.size(); // b.size() works ???? why??
}
Run Code Online (Sandbox Code Playgroud) 有人可以解释微妙的区别:
ofstream f("test.txt")
std::stringstream s;
s<<"";
f << s.rdbuf();
f.good() // filestream is bad!!
ofstream f("test.txt")
std::stringstream s;
s<<"";
f << s.str();
f.good() // is still ok!
Run Code Online (Sandbox Code Playgroud)
我主要使用.rdbuf()将字符串流推送到文件中(因为它更有效),但是如果字符串流为空而不是文件流变坏...?这不是傻瓜吗?我想我不太明白<< s.rdbuf()......
std::unordered_map < int, std::unordered_set<int> >
当我看看boost/serialization/map.hpp时,我尝试序列化/反
序列化它似乎很简单(甚至很难我不太理解它)以下代码似乎编译为序列化但无法反序列化.
有人知道如何正确地做到这一点,还是可以指出这些STL序列化技术的一些文档?我遗憾地找不到东西......
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <boost/serialization/map.hpp>
#include <boost/serialization/collections_save_imp.hpp>
#include <boost/serialization/collections_load_imp.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/split_free.hpp>
namespace boost { namespace serialization {
template<class Archive, typename... TArgs >
inline void save(Archive & ar, std::unordered_map<TArgs...> const&t, unsigned) {
boost::serialization::stl::save_collection<Archive, std::unordered_map<TArgs...> >(ar, t);
}
template<class Archive, typename... TArgs >
inline void load(Archive & ar, std::unordered_map<TArgs...> &t, unsigned) {
boost::serialization::stl::load_collection<Archive,
std::unordered_map<TArgs...>,
boost::serialization::stl::archive_input_map<
Archive, std::unordered_map<TArgs...> >,
boost::serialization::stl::no_reserve_imp<std::unordered_map<TArgs...> >
>(ar, …Run Code Online (Sandbox Code Playgroud) 我正在尝试深刻理解模板论证推导。我不明白的一点是,我应该如何将此处标准中的规则应用于
类型A和P
以下情况(遗憾的是 cppreference.com 上没有示例,请参阅下面的相关部分)
template<typename T>
void foo(T t);
void call_with_reference(int& r) {
foo(r)
}
Run Code Online (Sandbox Code Playgroud)
P没有参考类型:P := TA := int&-> 匹配P和A 给出:T推导为int&
这显然是错误的。标准中哪里有规定删除来自的引用A?一个不令人困惑、明确的明确答案将不胜感激。
我有一个关于这个问题的后续问题:符号P并A参考temp.deduct.call部分
如果我正确理解模板参数推导,则以下代码会发生以下情况:
template<typename T>
void foo(const T& a);
int b;
foo(std::move(b));
Run Code Online (Sandbox Code Playgroud)
P和。我们正在推论声明是引用(但不是转发引用)A的情况const T&A:std::move(b)类型为int&&[xvalue] -> 调整为A:= int( [7.2.2#1] )P:const T&-> 删除 const 和引用 ([ 12.9.2.1#3 ]) ->P:= TA- P > 结果T:= int。两个问题:
std::move(b)是一个表达式,我一直认为它的类型是int&&(因为std::move返回 a int&&),但是 ( [7.2.2#1] ) 告诉了一些不同的事情,这意味着在进行任何分析之前删除每个引用,所以当人们谈论表达式的类型时,从来没有涉及任何参考:struct A{ …Run Code Online (Sandbox Code Playgroud) 我想知道是否constexpr std::tuple可以在编译时对a进行排序:
template<typename T>
struct A{ T val; }; // a constexpr-enabled class
constexpr auto t = std::make_tuple( A<int>{3}, A<float>{1.f}, A<double>{2.0});
constexpr auto s = sort(t, [](auto&& v){return v.val; });
static_assert(std::is_same_v<std::tuple<A<float>,
A<double>,
A<int>>,decltype(s)>, "Wups");
Run Code Online (Sandbox Code Playgroud)
这可能吗,这里需要哪些构建块(std::sort是constexpr)?
(据我所知)在运行时不可能做到这一点,因为在运行时无法确定排序元组的类型。如果构建所有排列的类型映射,我也看不到一种方法,实际排列仍然只在运行时才知道。
回答/sf/answers/3220421851/ 给出了一些提示,它应该在编译时工作,但是如何?
为什么这样呢?
struct A { int a };
using B = const A; // or typedef const A B;
Run Code Online (Sandbox Code Playgroud)
decltype(B::a)评估int与否const int.我错过了如何在一生中学习C++的哪一章?