我有一个类模板,我似乎无法弄清楚如何执行Variadic模板样式实例化.
这是迄今为止我正在寻找的"代码":
template<typename _Classname, typename... Args>
class CFunctorStartExT
{
friend class CXXFactory;
protected:
template<typename U>
CFunctorStartExT(typename U& _functor, Args&... args) :
m_Functor(_functor),
m_args(args)
{
}
virtual bool ProcessLoop(CSomeClass* pThread)
{
return m_Functor(pThread, m_args);
}
protected:
_Classname& m_Functor;
Args... m_args;
};
Run Code Online (Sandbox Code Playgroud)
显然这不会编译:).我们的想法是创建一个类,它可以在构造函数中存储传入的值(如果有的话......它可能只有_Classname/U定义),以便稍后可以检索它们以传递给另一个函数中的m_Functor.
第一:Variadic模板甚至可以在VS2010中完成吗?我只是error C2143: syntax error : missing ',' before '...'从行中的模板声明得到编译问题template<typename _Classname, typename... Args>
第二,我想要完成的事情可以做到吗?谢谢!
在我们的项目中,我们决定使用下划线为成员变量和一些私有/受保护方法添加前缀(因此使用" _").
在讨论期间,有人声称这是不鼓励的,因为某些平台上的某些编译器/链接器存在某些不兼容性.因为我们希望尽可能便携,所以我想确定.
我还认为在C中用下划线加上前缀全局变量可能是个问题.
这同样适用于C++ - 链接,如果适用,在哪些情况下(平台/编译器/链接器)?
或多或少的标题暗示.虽然我还没有使用C++ 0x,但是我想在它发生时做好准备,而且我还想减少我必须重写的代码量才能使用它的一些功能.这样我就可以一次性地向前和向后兼容.
我发现的最有趣的一个是nullptr,我最近经常使用它.
在检查了"官方解决方法"和Meyer的建议之后,我决定在我的C++和未来的C++ 0x程序中使用它.第二部分很简单 - 作为关键字,nullptr只需支持.但第一部分让我感到有些不适.
Meyers提案的功能如下:
class nullptr_t { // ? this is my issue
// definition of nullptr_t
} nullptr = { };
Run Code Online (Sandbox Code Playgroud)
该提议的问题在于它声明了要声明为std::nullptr_tC++ 0x所需的类型.这意味着对于"感觉原生"的解决方法,必须通过重新打开std::命名空间来添加类型.我理解在C++程序中这是非法的(不像添加特殊化,这显然是皱眉和放开警告).
我想用nullptr在舒适和在C++程序合法的方式.我想到的一个选项是在另一个命名空间中声明类型然后使用它using:
namespace mylibrary {
class nullptr_t {
....
} nullptr = { };
// end namespace
}
// These would have to go in the header file.
using mylibrary::nullptr;
using …Run Code Online (Sandbox Code Playgroud) 基类具有不完整的类型
这个错误究竟是什么意思,我该如何解决?我试图通过class Entity在我的EntityPhysics标题中声明该类来声明该类,但它不起作用.
这是我的Entity.h
#ifndef __Game__Entity__
#define __Game__Entity__
#include <iostream>
#include <string>
#include "OGRE/Ogre.h"
#include "OgreInit.h"
class Entity{
public:
Entity(std::string entityId, std::string mesh, Ogre::Vector3 position = Ogre::Vector3::ZERO, Ogre::Vector3 rotation = Ogre::Vector3::ZERO);
virtual ~Entity() = 0;
void setPosition(Ogre::Vector3 position);
Ogre::Vector3 getPosition();
void setRotation(Ogre::Vector3 rotationIncrease);
Ogre::Vector3 getRotation();
void setMesh(std::string meshName);
std::string getMesh();
virtual void tick() = 0;
void removeEntity();
Ogre::Entity getEntity();
Ogre::SceneNode getSceneNode();
std::string entityId;
protected:
Ogre::Entity *ent;
Ogre::SceneNode *nod;
};
#endif /* defined(__Game__Entity__) */
Run Code Online (Sandbox Code Playgroud)
和我的EntityPhysics.h
#ifndef __Game__EntityPhysics__
#define __Game__EntityPhysics__ …Run Code Online (Sandbox Code Playgroud) 可能重复:
未定义参考
我现在一直在敲打这一套错误消息大约4个小时,我似乎无法弄明白.我之前没有在这里发帖,所以如果不是在正确的区域或者我做错了什么,我会提前道歉.无论如何,我收到的错误消息是:
main.cpp|28|undefined reference to `LinkedSortedList<Employee>::LinkedSortedList()'|
main.cpp|52|undefined reference to `empPrint(LinkedSortedList<Employee>&)'|
main.cpp|58|undefined reference to `empSave(LinkedSortedList<Employee>&, std::string)'|
main.cpp|65|undefined reference to `empLoad(LinkedSortedList<Employee>&, std::string)'|
main.cpp|70|undefined reference to `LinkedSortedList<Employee>::~LinkedSortedList()'|
main.cpp|70|undefined reference to `LinkedSortedList<Employee>::~LinkedSortedList()'|
obj\Debug\main.o||In function `Z9empSearchR16LinkedSortedListI8EmployeeE':|
main.cpp|109|undefined reference to `LinkedSortedList<Employee>::getHead()'|
Run Code Online (Sandbox Code Playgroud)
我的main.cpp如下:
#include <iostream>
#include <string>
#include <stdio.h>
#include <fstream>
#include "SortedList.h"
#include "LinkedSortedList.h"
#include "Employee.h"
#include "LinkedNode.h"
using namespace std;
void newEmp(LinkedSortedList <Employee>& empList);
void empSearch(LinkedSortedList <Employee>& empList);
void empPrint(LinkedSortedList <Employee>& empList);
void empSave(LinkedSortedList <Employee>& empList, string file);
void empLoad(LinkedSortedList <Employee>& empList, string file); …Run Code Online (Sandbox Code Playgroud) 有人可以解释各类型之间的差异uint8_t和__u8?
我知道这uint8_t是在stdint.h中定义的,并且它们在每个unix系统上都可用.
/* Unsigned. */
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
...
Run Code Online (Sandbox Code Playgroud)
如果我使用它们可以识别我打算做什么.
现在我偶然发现了__u8和__u16类型.它似乎对我来说是一样的.
其中一些类型在linux/types.h中定义
#ifdef __CHECKER__
#define __bitwise__ __attribute__((bitwise))
#else
#define __bitwise__
#endif
#ifdef __CHECK_ENDIAN__
#define __bitwise __bitwise__
#else
#define __bitwise
#endif
typedef __u16 __bitwise __le16;
typedef __u16 __bitwise __be16;
typedef __u32 __bitwise __le32;
...
Run Code Online (Sandbox Code Playgroud)
我没找到,__u8但我仍然可以使用它,它的行为就像uint8_t.
性能或内存消耗有一些差异吗?
感谢帮助 :)
我在使用chrono编译时遇到问题,这里是代码:
Time.hh
#include <chrono>
class Time
{
protected:
std::chrono::steady_clock::time_point _start_t;
std::chrono::steady_clock::time_point _now;
std::chrono::steady_clock::time_point _time;
public:
Time();
Time(const Time &other);
Time &operator=(const Time &other);
~Time();
public:
void start();
double getDurSec();
double getDurMilSec();
private:
void setNow();
};
Run Code Online (Sandbox Code Playgroud)
编译错误:
g++ -W -Wall -Wextra -I./include -std=c++0x -c -o src/Time/Time.o src/Time/Time.cpp
In file included from src/Time/Time.cpp:11:0:
./include/Time/Time.hh:21:3: error: ‘steady_clock’ in namespace ‘std::chrono’ does not name a type
./include/Time/Time.hh:22:3: error: ‘steady_clock’ in namespace ‘std::chrono’ does not name a type
./include/Time/Time.hh:23:3: error: ‘steady_clock’ in namespace ‘std::chrono’ does …Run Code Online (Sandbox Code Playgroud) 我在C项目中遇到了以下构造,我将其移植到C++;
enum TestEnum
{
A=303,
B=808
} _TestEnum;
int foo()
{
_TestEnum = B;
}
Run Code Online (Sandbox Code Playgroud)
在使用GCC进行编译并查看生成的代码时,我得到:
nils@doofnase ~ $ gcc -std=c90 -O2 -c ./test.c -o test.o
nils@doofnase ~ $ size test.o
text data bss dec hex filename
59 0 0 59 3b test.o
Run Code Online (Sandbox Code Playgroud)
因此使用零字节数据或BSS段.
另一方面,如果我用C++编译,我得到:
nils@doofnase ~ $ g++ -std=c++11 -O2 -c ./test.c -o test.o
nils@doofnase ~ $ size test.o
text data bss dec hex filename
59 0 4 63 3f test.o
Run Code Online (Sandbox Code Playgroud)
我看到BSS中分配了四个字节的存储空间,正如我所料.
此外,在C项目中,枚举定义实际上位于头文件中,该头文件包含在多个c文件中.该项目编译和链接就好了.当编译并链接为C++时,编译器会抱怨_TestEnum是在多个对象中定义的(就这样!).
这里发生了什么?我在看一些古老的C语言特例吗?
编辑:为了完整起见,这是gcc版本:
nils@doofnase ~ $ gcc …Run Code Online (Sandbox Code Playgroud) 根据规范,不允许使用带有前导下划线的全局名称:
17.4.3.1.2全局名称
- 以下划线开头的每个名称都保留给实现,以用作全局名称空间中的名称.
这是否也适用于顶级匿名命名空间中定义的名称?
该<windows.h>头带有自己的BOOL类型.窥视实现,它似乎FALSE只是一个宏0,并且TRUE只是一个宏1,但我不确定这是否已指定.
将a转换BOOL为a 的惯用方法是bool什么?我可以想象很多可能的方法:
bool a = static_cast<bool>(x);
bool b = x ? true : false;
bool c = (x == TRUE);
bool d = (x != FALSE);
bool e = !!x;
// ...
Run Code Online (Sandbox Code Playgroud)