我们有一个使用许多C++ 11工具的项目,我们考虑过这个技巧,使它在C++ 03上编译.
#ifndef USE_CPP0X
# define override
#endif
Run Code Online (Sandbox Code Playgroud)
据我所知,禁止定义C++关键字,这是合法的吗?
我不明白模板参数之间的区别
template <class T>
class C
{
T t;
};
void foo()
{
C<void ()> c1; //isn't compiled
C<void (*)()> c2;
}
Run Code Online (Sandbox Code Playgroud)
void()的类型是什么?这种类型用于boost :: function ..
在C++ 03中,我想创建一个std :: set,在迭代时,首先出现一个整数,之后,我不关心什么顺序,但是我需要一个排序来确保没有重复组.例如,如果我有一组年份,并且在迭代时我想要在所有其他年份之前处理2010年.
std::set<int> years;
// I do not know the set of years up front, so cannot just make a vector, plus
// there could potentially be duplicates of the same year inserted more than
// once, but it should only appear once in the resultant set.
years.insert(2000);
years.insert(2001);
years.insert(2010);
years.insert(2011);
years.insert(2013);
for (std::set<int>::iterator itr = years.begin(); itr != years.end(); ++itr) {
process_year(*itr);
}
Run Code Online (Sandbox Code Playgroud)
基本上,我需要提供一个比较器,在运行时已知的某一年(例如2010年)与其他年份的比较少,但剩余的年份是按顺序排序的,但没有按任何必要的顺序排列,只是为了确保没有重复.组.
我正在使用Visual C++ 2008.我无法使用,pop_back因为它成为C++ 11中字符串类的成员函数.
因为我不能使用,我可以使用pop_back什么?
如何编译此代码:
#include <iostream>
using namespace std;
enum E { A, B};
template< E x>
class C {
public:
#if( x == A)
static void foo() {
cout << "A";
}
#elif( x == B)
static void goo() {
cout << "B";
}
#endif
};
int main() {
C< A>::foo();
C< B>::goo();
return 0;
}
error: ‘goo’ is not a member of ‘C<(E)1u>’
Run Code Online (Sandbox Code Playgroud)
我有两个大的类只有几行不同,所以我想制作枚举模板.问题是这些行中有using关键字,所以我不知道该放什么.
有没有正确的方法呢?
PS>我必须使用C++ 03.
编辑:一点澄清.我知道template<>构造,但我不想使用它,因为这样我得到了很多代码重复.也许我可以以某种方式进行部分"模板实例化"(如果这是正确的术语template<>)?
假设我有两个类(我不会有更多):
class A {
public:
//…a …Run Code Online (Sandbox Code Playgroud) 我有一个类层次结构,可以使用它写入对象operator<<.该示例如下所示:
#include <iostream>
struct Base
{
};
struct Derived : public Base
{
};
struct Shift
{
template< typename U >
Shift& operator<<(const U&)
{
std::cerr << __PRETTY_FUNCTION__ << std::endl;
return *this;
}
Shift& operator<<(const Base&)
{
std::cerr << __PRETTY_FUNCTION__ << std::endl;
return *this;
}
#if 0
Shift& operator<<(const Derived&)
{
std::cerr << __PRETTY_FUNCTION__ << std::endl;
return *this;
}
#endif
};
int main()
{
Shift sh;
Base bas;
Derived der;
int u32 = 0;
sh << bas;
sh …Run Code Online (Sandbox Code Playgroud) 我有一个std::vector<Person> v与
struct Person
{
Person(int i,std::string n) {Age=i; this->name=n;};
int GetAge() { return this->Age; };
std::string GetName() { return this->name; };
private:
int Age;
std::string name;
};
Run Code Online (Sandbox Code Playgroud)
我需要转变为 std::map<std::string,int> persons
在编写类似这样的代码时我遇到了困难:
std::transform(v.begin(),v.end(),
std::inserter(persons,persons.end()),
std::make_pair<std::string,int>(boost::bind(&Person::GetName,_1)), (boost::bind(&Person::GetAge,_1)));
Run Code Online (Sandbox Code Playgroud)
在c ++ 03中使用stl算法转换vector<Person> v成最佳方法是什么map<std::string,int> persons?
我是C++的新手,我很难理解这段代码:
template <typename T = unsigned>
Run Code Online (Sandbox Code Playgroud)
T = unsigned意思?unsigned对给定类型强制执行?我有int* foo[SIZE],我想搜索它指向的第一个元素NULL.
但是当我这样做时:
std::find(foo, foo + SIZE, NULL)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
错误C2446:'==':没有从'const int'转换为'int*'
我应该只使用static_cast<int*>(NULL)而不是NULL?
C++ 11解决了这个问题,nullptr但在C++ 03中我不是一个选择
假设我使用一个函数(来自第三方API,在另一个名称空间中),函数由函数填充一些信息:
double foo::f(double x, double& info);
Run Code Online (Sandbox Code Playgroud)
当我调用它时,我必须创建一个局部变量,即使我不关心填充值:
const double x = 3.14159;
double info = 0.0;
const double y = foo::f(x, info);
// Nothing to do with info
Run Code Online (Sandbox Code Playgroud)
然后,我想将此函数接口以具有可选参数而不是引用.是否有比以下更常见/更好/最简单/更易读的方式?
double ff(double x, double* info = 0)
{
double unused_info = 0.0;
double& info_ = ((bool)info ? *info : unused_info);
return foo::f(x, info_);
}
Run Code Online (Sandbox Code Playgroud)
这有效,但对于需要看起来有点笨拙.
注意:出于兼容性原因,我不使用C++ 11.但我对C++ 11解决方案很感兴趣,即使它没有回答这个问题.
c++ ×10
c++03 ×10
templates ×3
c++11 ×1
casting ×1
comparator ×1
enums ×1
if-statement ×1
null ×1
nullptr ×1
overloading ×1
stdset ×1
stl ×1
vector ×1