我一直在阅读关于>>嵌套模板和>>移位运算符的结束...
现在我已经在我的MSVS2010中尝试过了,没有问题.
std::map<int, std::pair<int, int>> m;
这段代码正是我想要的(对的映射),但我应该得到一些错误 >>
这些天编译器更聪明?
我正在尝试为std :: bitset编写bool转换运算符
我试过了:
template<size_t size>
operator bool(std::bitset<size> & b)
{
return b.any();
}
Run Code Online (Sandbox Code Playgroud)
但我明白了
error C2801: 'mynamespace::operator bool' must be a non-static member
Run Code Online (Sandbox Code Playgroud)
来自我的视觉工作室.
但是,当我查找C2801解释时,它没有说明转换运算符(仅约=, - >,[],())
那么,有可能以某种方式写"将std :: bitset转换为bool运算符吗?"
(我不能b.any()在我的if语句中调用,因为当std :: bitset被unsigned或者某些东西替换时,必须运行相同的代码
typedef std::bitset<x> Bitset;
//typedef unsigned Bitset;
Run Code Online (Sandbox Code Playgroud)
所以理想的语法就像:
Bitset b = whatewer;
if(b)
doStuff();
Run Code Online (Sandbox Code Playgroud)
)
如果无法进行此重载,建议的解决方法是什么?
到目前为止,我使用它像:
if(b == Bitset(0))
doStuff();
Run Code Online (Sandbox Code Playgroud)
但我不喜欢它.
谢谢
有下面的代码,为什么第一次分配不调用模板operator=的Foo,但第二个呢?这里有什么?即使用户定义的模板存在,是否有编译器生成的第一个赋值?
#include <iostream>
using namespace std;
struct UberFoo { };
struct Foo : public UberFoo
{
template<typename T> void operator=(const T& v) { cout << "const T&" << endl; Set(v); }
template<typename T> void operator=(T& v) { cout << "T&" << endl; return Set(v); }
virtual void Set(const Foo&) { cout << "Foo::Set(const Foo&)" << endl; }
virtual void Set(const UberFoo&) { cout << "Foo::Set(const UberFoo&)" << endl; }
};
struct Bar : public Foo
{ …Run Code Online (Sandbox Code Playgroud) 我正在尝试在编译时将月份从__DATE__格式转换Mmm为数字.我需要它用于gcc和MS VC 2012.
我的尝试:
template <char C0, char C1, char C2>
struct month{};
template<> struct month < 'J', 'a', 'n'> { static const unsigned id = 1; };
template<> struct month < 'S', 'e', 'p'> { static const unsigned id = 9; };
static const char c0 = __DATE__[0];
static const char c1 = __DATE__[1];
static const char c2 = __DATE__[2];
static const unsigned currId = month<c0, c1, c2>::id; //this gives error
static const unsigned currId2 …Run Code Online (Sandbox Code Playgroud) 这样做是否正确?
typedef unsigned int Index;
enum
{
InvalidIndex = (Index) -1
};
Run Code Online (Sandbox Code Playgroud)
我已经读过它在各个平台上都不安全,但我在很多"专业"代码中看到了这个...
我想从字符串生成int并能够生成它.类似哈希函数但是双向函数.我想在我的应用程序中使用int作为ID,但希望能够在记录或调试时将其转换回来.
喜欢:
int id = IDProvider::getHash("NameOfMyObject");
object * a = createObject(id);
...
if(error)
{
LOG(IDProvider::getOriginalString(a->getId()), "some message");
}
Run Code Online (Sandbox Code Playgroud)
我听说过略微修改的CRC32快速且100%可逆,但我找不到它,我自己也无法写.
任何提示我应该使用什么?谢谢!
编辑 我刚刚创建了源代码我有完整的CRC32来自:
Jason Gregory:游戏引擎架构
报价:
"与任何哈希系统一样,冲突是可能的(即,两个不同的字符串可能最终使用相同的哈希码).但是,通过合适的哈希函数,我们可以保证所有合理的输入字符串都不会发生冲突我们可能会在我们的游戏中使用.毕竟,一个32位的哈希代码表示超过40亿个可能的值.所以如果我们的哈希函数在这个非常大的范围内很好地分配字符串,我们就不太可能发生冲突. Naughty Dog,我们使用了CRC-32算法的变体来对我们的字符串进行哈希处理,并且在Uncharted:Drake's Fortune的两年多的开发中我们没有遇到过一次冲突.
我有World管理对象创建的类...在创建之后调用afterCreation方法,我创建的对象是从Entity(例如MyEntity)派生的用户定义类型,我想调用addEntity.我的对象是别的,我什么都不做.
addEntity必须使用适当的调用T,因为它为每个派生类等生成唯一的ID.
这是我的解决方案:
template <int v>
struct ToType
{
enum { value = v };
};
template <typename T>
void World::afterCreation(T * t)
{
afterCreation(t, ToType<std::is_base_of<Entity, T>::value>());
}
template <typename T>
void World::afterCreation(T * t, ToType<true>)
{
addEntity(t); //here I cant pass Entity *, I need the real type, eg. MyEntity
}
template <typename T>
void World::afterCreation(T * t, ToType<false>)
{
}
Run Code Online (Sandbox Code Playgroud)
我的问题是 - 可以做得更好吗?
如何在没有ToType或类似的情况下模拟以下代码?
template …Run Code Online (Sandbox Code Playgroud) 当我运行cmake-gui并点击配置时,我可以为我的项目指定生成器。我可以选择 Visual Studio 解决方案、Eclipse、makefile 等...
我在 64 位系统上,想要构建 64 位应用程序。到目前为止,我使用 Visual Studio 10 Win64 生成器来生成我的解决方案。不,我想(必须)用 makefile 构建它。但没有32b/64b选项。
export CFLAGS=-m32默认情况下(在 64b 系统上)使用 64 位构建的 makefile,只有在使用我的 cmake 文件中的内容后才使用 32b 的 makefile ?
tldr
有没有办法,静态检测以下代码中的错误?
struct Foo
{
operator const int*()
{
return &data;
}
int data;
};
int main()
{
Foo f;
delete f;
}
Run Code Online (Sandbox Code Playgroud)
由于存在从富转换到void*只包含一个用户定义的转换,它实际上是允许调用delete上f.
更长的故事
在我们的代码库中,在伪代码中有一种非常愚蠢的反序列化字符串的方法
char * buff = bar.loadString();
use buff;
delete buff;
Run Code Online (Sandbox Code Playgroud)
方式被改变为一个模板化的加载函数,所以现在反序列化看起来像
bar.load(m_IntMember);
bar.load(m_StringMember);
Run Code Online (Sandbox Code Playgroud)
但所有事件(有很多)loadString都必须手动更改,如下所示:
string buff;
bar.load(buff);
use buff;
Run Code Online (Sandbox Code Playgroud)
我们都知道人为错误会导致什么,所以有些地方代码被错误地修改了
string buff;
bar.load(buff);
use buff;
delete buff; //notice the delete
Run Code Online (Sandbox Code Playgroud)
因为我们使用它有点非标准的实现string它实际上有一个overloaded const char * operator,可以被转换为void*可以删除...
我想在编译时捕获所有这些错误(我们有自定义的高性能分配器,因此在运行时,很容易破坏内存而不会出现任何运行时错误)
我不能声明全局删除操作符接受const char*
我不能暂时删除删除操作符,string因为它被大量使用,所以没有它就无法编译(我不能"过滤掉"所有错误中描述的错误,因为msvc在达到一定数量的时候停止编译错误)
我能做什么?
有没有办法检查是否class有typedef哪个有效private typedef?
以下代码适用于VS2013,但在ideone的gcc上失败
template<typename T>
struct to_void
{
typedef void type;
};
class Foo
{
typedef int TD;
};
template <typename T, typename dummy = void>
struct has_TD : std::false_type {};
template <typename T>
struct has_TD<T, typename to_void<typename T::TD>::type > : std::true_type{};
int main()
{
std::cout << std::boolalpha << has_TD<Foo>::value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
编辑 - 为什么我想要这个
我有自定义序列化系统,可以序列化任意类型.当它必须表现不同时(例如字符串),它有几个重载.对于其余类型,它只是将值写入内存中.如果我有编写类型,我有时也可以写入内存(保存和加载发生在同一架构上,使用相同的编译器编译,因此填充将是相同的,等等).此方法对于POD类型(std::is_pod特征)有效,但所有POD类型仅是所有类型的子集,支持此序列化.
所以我基本上都有模板化的函数write<T>,它只是写sizeof(T)字节(原始序列化)...但是我不希望这个被错误地调用,我想要用户,在他们的类中明确地说:"这个类/结构可以是原始的序列化过").我这样做的方法是ALLOW_RAW_SERIALIZE定义一些可以通过trait检查的typedef 的宏.如果类MyClass不包含typedef,则调用write(myClassInstance)将产生编译器错误.
基本决定类是否可以原始序列化的东西是它的成员(没有反射,成员不能自动枚举和检查,因此用户必须提供这样的信息).典型的类看起来像这样: …
c++ ×9
templates ×3
64-bit ×1
bit-shift ×1
cmake ×1
hash ×1
type-traits ×1
unsigned ×1
visual-c++ ×1