我最近决定将指针向量更改为智能指针向量,但尽管这些智能指针与STL兼容,但我无法转换某些算法来使用它们.
考虑一下
class Base
{
...
virtual bool valid();
};
Run Code Online (Sandbox Code Playgroud)
你怎么转换
bool is_any_valid( vector< Base* > v )
{
return v.end() !=
find_if( v.begin(), v.end(), mem_fun( &Base::valid ) );
}
Run Code Online (Sandbox Code Playgroud)
进入这个?
bool is_any_valid( vector< my_smart_ptr< Base > v )
{
// mem_fun() fails, because valid() is not a member of my_smart_ptr< Base > !!!
// return v.end() !=
// find_if( v.begin(), v.end(), mem_fun( &Base::valid ) );
}
Run Code Online (Sandbox Code Playgroud)
您可以假设my_smart_pointer <>与shared_ptr <>基本上具有相同的接口,但我不能在我的项目中使用boost.
是否有一个(通用)适配器,我可以写,以允许mem_fun或mem_fun_ref工作?我最好寻找一个在线解决方案,例如:
find_if( v.begin(), v.end(), mem_fun( some_adapter( &Base::valid ) ) ); …Run Code Online (Sandbox Code Playgroud) 我们有一个包含C和C ++代码的大型项目。
对于每个C ++实现,除了C ++头文件之外,我们通常还会提供一个C头文件,以使功能也可用于.c文件。
因此,我们的大多数文件如下所示:
foo.hpp:
class C {
int foo();
};
Run Code Online (Sandbox Code Playgroud)
foo.h:
#ifdef __cplusplus
extern "C" {
typedef struct C C; // forward declarations
#else
class C;
#endif
int foo( C* ); // simply exposes a member function
C* utility_function( C* ); // some functionality *not* in foo.hpp
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
foo.cpp:
int C::foo() { /* implementation here...*/ }
extern "C"
int foo( C *p ) { return p->foo(); }
extern "C"
C* utility_function ( C* ) …Run Code Online (Sandbox Code Playgroud) John Lakos 将此问题称为编译时耦合的潜在根源(他的简介中的图 0-3):
我面临的问题是编译了太多文件,因为对单个枚举存在物理依赖性。
我有一个带有枚举定义的标头:
// version.h
enum Version {
v1 = 1,
v2, v3, v4, v5, ... v100
};
Run Code Online (Sandbox Code Playgroud)
这被数百个文件使用。每个文件定义一类对象,必须使用该read()函数从磁盘读取这些对象。Version用于确定读取数据的方式。
每次引入新的类或类成员时,都会将新条目附加到枚举中
// typeA.cpp
#include "version.h"
void read (FILE *f, ObjectA *p, Version v)
{
read_float(f, &p->x);
read_float(f, &p->y);
if (v >= v100) {
read_float(f, &p->z); // after v100 ObjectA becomes a 3D
}
}
Run Code Online (Sandbox Code Playgroud)
和
// typeB.cpp
#include "version.h"
void read (FILE *f, ObjectB *p, Version v)
{
read_float (f, &p->mass);
if …Run Code Online (Sandbox Code Playgroud) 我写了这个仿函数来执行和操作(&&):
// unary functor; performs '&&'
template <typename T>
struct AND
{
function<bool (const T&)> x;
function<bool (const T&)> y;
AND(function<bool (const T&)> xx, function<bool (const T&)> yy)
: x(xx), y(yy) {}
bool operator() ( const T &arg ) { return x(arg) && y(arg); }
};
// helper
template <typename T>
AND<T> And(function<bool (const T&)> xx, function<bool (const T&)> yy)
{
return AND<T>(xx,yy);
}
Run Code Online (Sandbox Code Playgroud)
注意它的构造函数参数类型: function<bool (const T&)>.
现在,我试图以各种方式(内部big_odd_exists())实例化它:
int is_odd(int n) …Run Code Online (Sandbox Code Playgroud) 为什么hash会将自身返回为哈希值?
我已经并排设置了一个哈希和一个哈希,而一个字符串的字符串按预期工作,而int一个产生自己的哈希值!
它应该如何工作?
hash<int> h;
for( int i=0 ; i<15 ; ++i )
{
cout << "int hash for " << i << " is " << h(i) << endl;
}
hash<string> hs;
for( int i=0; i<15; ++i) {
stringstream ss;
ss << i;
cout << "string hash for " << i << " is " << hs(ss.str()) << endl;
}
Run Code Online (Sandbox Code Playgroud)
结果是
+ g++-4.8 -std=c++11 -O2 -Wall -pedantic -Weffc++ -Wextra main.cpp
+ ./a.out
int hash for 0 is …Run Code Online (Sandbox Code Playgroud) 我们目前使用 C++11。
std::optionalC++11 中缺少它 - 它已在 C++17 中引入。幸运的是,我找到了一个std::optional与 C++11具有相同接口并在 C++11 中工作的实现(https://github.com/TartanLlama/optional)
这定义optional在namespace tl. 所以,我可以像这样使用它:
#include "optional.hpp"
tl::optional<int> to_int(string str) {
if (...) { return atoi(str); }
else { return tl::nullopt; }
}
Run Code Online (Sandbox Code Playgroud)
但是,我正在寻找一种方法来调用它,就好像std::optional可用一样:
#include "optional.hpp"
std::optional<int> to_int(string str) {
if (...) { return atoi(str); }
else { return std::nullopt; }
}
Run Code Online (Sandbox Code Playgroud)
这样,当我们切换到 C++17 时,我就不必将每个都替换tl::optional为std::optional. 我只需要将“optional.hpp”标头的内容替换为:
// optional.hpp
// ------------
#include <optional.hpp>
Run Code Online (Sandbox Code Playgroud)
今天,这个标头实现为:
// optional.hpp
// …Run Code Online (Sandbox Code Playgroud) 可能重复:
为什么C区分 - >和.?
程序员必须区分.和->访问对象成员时的真正原因是什么?
void foo( Point &p ) {
p.x ;
p->y; // syntax error
}
void foo( Point *p ) {
p.x ; // syntax error
p->y;
}
Run Code Online (Sandbox Code Playgroud)
我的意思是,无论如何,他们都指的是一个物体,p.
为什么我每次都要检查p是什么?编译器能不能理解?
为什么他们不允许它接受.两者的语法?这也适用于堆栈对象.
如果是由于C传统,
.和->访问者?超过15年,我一直谦卑地认为编译错误是我的错!
我最近决定整理一个头文件,它有很多定义,如下所示:
// api.h
template< typename T>
inline void func( T param )
{
// stuff here
}
Run Code Online (Sandbox Code Playgroud)
所以我想把它变成:
// api.h
#include "api_details.h"
template< typename T>
inline void func( T param )
{
return details::func( param );
}
// api_details.h
namespace details {
template< typename T>
inline void func( T param )
{
// stuff here
}
}
Run Code Online (Sandbox Code Playgroud)
希望这inline不会增加我正在执行的额外副本的成本.
虽然' C++中的答案做内联函数会阻止复制吗?"似乎暗示没有复制,这个问题出现了:
如果内联不复制函数参数,那么下面的行为会不会很糟糕?
inline void change_value( int i ) {
i++;
}
...
int x=5;
change_value(x);
assert(x==5); …Run Code Online (Sandbox Code Playgroud) 我创建了这个函子,Functions它用于存储一些函数,std::functions并按顺序调用它们,就像它std::function本身一样:
typedef std::function <void (int)> FuncType;
struct Functions
{
vector<FuncType> m;
void operator+= (FuncType f) {
m.push_back(f);
}
void operator() (int param ){ // same prototype as FuncType
vector<FuncType>::iterator it;
for(it=m.begin(); it!=m.end(); ++it) {
(*it)(param);
}
}
};
Run Code Online (Sandbox Code Playgroud)
它非常有用,因为它可以存储在FuncType:
int main()
{
Functions f;
f += foo;
f += bar;
FuncType ptr(f);
ptr(10); // calls foo(10), then bar(10)
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我希望能够使这成为一个模板化的算子.但我想不出一种方法来operator()跟随功能的原型:
template <typename FuncType> // for any Function type
struct Functions …Run Code Online (Sandbox Code Playgroud) 我正在制作一个自定义迭代器,但我很困惑的是,在模板化迭代器实现的情况下,编译器无法识别pointer或者reference类型定义.
以下代码编译正常:
#include <iterator>
struct It : std::iterator<std::random_access_iterator_tag, int>
{
int *v;
pointer operator-> () { return v; }
reference operator* () { return *v; }
};
Run Code Online (Sandbox Code Playgroud)
但是一旦我将我的自定义迭代器模板化,我就会收到错误:
#include <iterator>
template <class T>
struct It2 : std::iterator<std::random_access_iterator_tag, T>
{
T *v;
pointer operator-> () { return v; }
reference operator* () { return *v; }
};
Run Code Online (Sandbox Code Playgroud)
error: 'pointer' does not name a type
note: (perhaps 'typename std::iterator<std::random_access_iterator_tag, T>::pointer' was intended)
error: 'reference' does not name …Run Code Online (Sandbox Code Playgroud) 我希望能够将类的成员函数调用“转发”到该类的每个成员变量:
class MyObject {
X_Behavior v1;
X_Behavior v2;
...
Y_Behavior v10;
Z_Behavior v11;
...
public:
void clear() { v1.clear(); v2.clear(); ... v10.clear(); v11.clear(); }
void hide() { v1.hide(); v2.hide(); ... v10.hide(); v11.hide(); }
void show() { v1.show(); v2.show(); ... v10.show(); v11.show(); }
};
Run Code Online (Sandbox Code Playgroud)
所有这些功能都根据预期的“行为”在每个组件类中实现。例如
class X_Behavior {
public:
void clear();
void hide();
void show();
...
};
Run Code Online (Sandbox Code Playgroud)
手动复制如果这些迭代
void clear() { v1.clear(); v2.clear(); ... v10.clear(); v11.clear(); }
void hide() { v1.hide(); v2.hide(); ... v10.hide(); v11.hide(); }
void show() { v1.show(); v2.show(); …Run Code Online (Sandbox Code Playgroud)