我有一个模板类,Iterable; 我想要重载begin()和end()自由功能.它存储的数据作为vector的unique_ptr,但接口使用boost::indirect_iterator的便利性.
我的代码构建和运行CLang-3.5,但我试过g++-4.9,它没有.但我不知道为什么?(以及哪种编译器具有正确的行为).
template<typename T>
using SimpleVec = std::vector<T, std::allocator<T>>;
template <typename T,
template <typename> class Container = SimpleVec,
class String = std::string>
class Iterable
{
template <typename friendT,
template <typename> class friendContainer,
class friendString>
friend boost::indirect_iterator<typename friendContainer<std::unique_ptr<friendT>>::iterator>
begin(Iterable<friendT, friendContainer, friendString>& i);
template <typename friendT,
template <typename> class friendContainer,
class friendString>
friend boost::indirect_iterator<typename friendContainer<std::unique_ptr<friendT>>::iterator>
end(Iterable<friendT, friendContainer, friendString>& i);
};
Run Code Online (Sandbox Code Playgroud)
和免费功能:
template <typename T,
template …Run Code Online (Sandbox Code Playgroud) Scott Mayers的"Effective Modern C++"中的第13项声明更喜欢const_iterator而不是迭代器.我同意,但我也想使用非成员函数而不是成员函数.根据这本书,应该有一个非成员函数std::cbegin()和std::cend()C++ 14.
为了利用这个功能,我刚刚安装了gcc版本4.9.2并使用该标志进行了编译-std=c++14.它似乎编译,直到我尝试使用std::cbegin().我开始寻找对此功能的支持,但无法找到任何相关信息.例如,在gnu onlinedocs状态下,甚至没有提到该功能.
我的问题是,将在c ++ 14中支持std::cbegin()并std::cend()真正得到支持,还是这本书中的错误?如果它是C++ 14的特性,是否有已经支持这些功能的编译器以及gcc什么时候支持它?
在SO上有很多问题,begin()但这些问题是关于成员函数或关于constexpr-ness而不是关于非成员变体的支持.
我有一组多态类,例如:
class Apple {};
class Red : public Apple {};
class Green : public Apple {};
Run Code Online (Sandbox Code Playgroud)
以及比较它们的免费功能:
bool operator==(const Apple&, const Apple&);
bool operator< (const Apple&, const Apple&);
Run Code Online (Sandbox Code Playgroud)
我正在设计一个可复制的包装类,它允许我在STL映射中使用类Red和Green键,同时保留它们的多态行为.
template<typename Cat>
class Copy
{
public:
Copy(const Cat& inCat) : type(inCat.clone()) {}
~Copy() { delete type; }
Cat* operator->() { return type; }
Cat& operator*() { return *type; }
private:
Copy() : type(0) {}
Cat* type;
};
Run Code Online (Sandbox Code Playgroud)
我希望这种Copy<Apples>类型尽可能地互换Apples.还有一些我必须添加到Copy上面的类中的函数,但是现在我正在使用一个自由函数operator==,如下所示:
template<typename …Run Code Online (Sandbox Code Playgroud) 在boost.geometry的文档中,它说明了
注意:更喜欢使用x = bg :: get:<0>(point1);
(而不是x = point1.get <0>();)
我在boost docs的其他地方看过这个.我的问题是为什么?这是最好的做法,表演还是一些怪癖?这是一般规则还是特定于此库?
简单的问题,这里:静态成员函数之间的区别是什么,即可以在不需要对象访问它的情况下调用的函数(简单地使用类标识符)和非成员函数?在这里,我在概念上和功能上都要求.
非成员函数在概念上是静态的吗?
下面是头文件中的一些简化代码,其中声明了自由函数但未定义,并且声明并定义了向量。
cpp 文件包含自由函数的实现。
我想知道是否有办法在头文件中声明向量并将定义放在 cpp 文件中。
// my-file.h
namespace MyNamespace
{
bool foo(const std::string& name, const std::string& value);
void bar(const std::string& name, const std::string& value);
const std::vector<std::function<void(const std::string&, const std::string&)>> m_myVector
{
foo,
bar,
[](const std::string& name, const std::string& value)
{
// do some stuff
}
};
} // MyNamespace
Run Code Online (Sandbox Code Playgroud) it++; // OK : Widely used expression for moving iterator.
it_prev = it-1; // ERROR : What I expected; + - operators never existed
it_prev = std::prev(it) // OK
it_next3 = it+3; // ERROR : also, nothing like this exists
it_next3 = std::next(it,3) // OK
Run Code Online (Sandbox Code Playgroud)
为什么 Iterator 类没有 + - 运算符作为成员函数?
或者std::prev()作为成员函数来做到这一点?
it_prev = it.prev() // nothing like this
Run Code Online (Sandbox Code Playgroud)
定义prev迭代器外部的函数是否有特殊原因?
我有一个自由函数,foo在名称空间N中定义。的标头foo位于全局包含搜索路径中,因此任何人都可以通过包括来调用它foo.h。
foo调用另一个本地自由函数foo1,该函数在中定义foo.cpp。
// foo.h
namespace N
{
void foo();
}
// foo.cpp
#include "foo.h"
namespace
{
// declare in the unnamed namespace?
void foo1()
{
// do some stuff
}
}
namespace N
{
// declare in namespace N?
void foo1()
{
// do some stuff
}
void foo()
{
foo1();
}
} // N
Run Code Online (Sandbox Code Playgroud)
我应该放入foo1未命名的名称空间还是namespace N?还是没关系?
更新
我想将范围限制foo1为foo.cpp。
非mumber函数可以多次delcared,而成员函数只能声明一次?这是正确的吗 ?我的例子似乎说是的.
但为什么 ?
class Base{
public:
int foo(int i);
//int foo(int i=10); //error C2535: 'void Base::foo(int)' : member function already defined or declared
};
//but it seems ok to declare it multiple times
int foo(int i);
int foo(int i=10);
int foo(int i)
{
return i;
}
int main (void)
{
int i = foo();//i is 10
}
Run Code Online (Sandbox Code Playgroud) 如何将自己的方法添加到预先存在的类中,而不会对预先存在的类的上下文进行任何更改。
例如 :
A.hpp
class A
{
public :
void print1()
{
cout << "print1";
}
};
B.hpp
//add a helper function to class A
//for example:
A::print2()
{
cout << "print2";
}
main.cpp
#include "A.hpp"
#include "B.hpp"
main()
{
A a1;
a1.print2();
}
Run Code Online (Sandbox Code Playgroud)