我正在编写一个模板类,它包含成员函数以减少一些调用 - 如果某些条件为真,则不需要调用成员函数.签名看起来像这样
template <typename MemFuncType, MemFuncType> class MemberWrapper;
Run Code Online (Sandbox Code Playgroud)
我可以专门研究它:
template <typename R, typename T, R T::* MemFunc> class MemberWrapper<R T::*, MemFunc>{};
Run Code Online (Sandbox Code Playgroud)
我还想限制参数的数量R T::*.我该怎么做呢?
我能想到的唯一解决方案是通过提供基于返回类型,函数类型,参数列表和cv限定符的部分特化来实现成员函数特征类.这将导致像当前std::mem_fn 重载一样繁琐的实现.有没有办法做得更好?
编辑:更改Ret到R.正如评论中指出的那样,它并不是真正的返回类型,并且专业化无效.
这是我一直在考虑的事情.我已经做了一些研究而且找不到任何东西,但我也没有发现任何相反的东西.
考虑一下这个std::sort功能<algorithm>.它需要两个迭代器和一个函数指针作为参数.所以,如果我想按字母顺序对字符串向量进行排序,我会这样做:
bool ascending(std::string lhs, std::string rhs) { return lhs < rhs; }
std::sort(my_vector.begin(), my_vector.end(), ascending);
Run Code Online (Sandbox Code Playgroud)
问题是这种类型的排序函数区分大小写,因此在以大写"Z"开头的字符串后面会放置以小写"a"开头的字符串.我看到的唯一可见的解决方案是创建一个额外的功能bool ascending_case_insensitive().但是,如果我可以在sort中使用bool ascending()带有附加bool is_case_sensitive参数的函数,那就太好了.这可能吗?
我目前正在用AI制作一个井字游戏程序,我在翻译这行代码(python)时遇到了一些麻烦:
RANKS = dict([(4,3), # center = 3
(0,2),(2,2),(6,2),(8,2), # corners = 2
(1,1),(3,1),(5,1),(7,1)]) # sides = 1
Run Code Online (Sandbox Code Playgroud)
进入C++
有什么建议?
我有以下traits class(IsLexCastable)来检查是否可以通过调用将类型转换为字符串boost::lexical_cast<string>.它错误地返回true了vector<int>.
#include <iostream>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
namespace std
{
/// Adding to std since these are going to be part of it in C++14.
template <bool B, typename T = void>
using enable_if_t = typename std::enable_if<B, T>::type;
}
template <typename T, typename = void>
struct IsLexCastable : std::false_type
{
};
template <typename T>
struct IsLexCastable<T, std::enable_if_t<std::is_same<std::string, decltype(boost::lexical_cast<std::string>(std::declval<T>()))>::value> > …Run Code Online (Sandbox Code Playgroud) 我有一个mpl :: vector并希望使用矢量元素作为模板参数来实例化模板.这是怎么做到的?可以使用参数包来合并额外的mpl :: vector元素吗?
例如:
struct A; struct B; struct C; struct D;
using args = mpl::vector<A, B, C, D>;
template<typename argA, typename argB, typename argC...>
struct derived_type;
using type_from_vector = derived_type<args>;
Run Code Online (Sandbox Code Playgroud)
接近这样的事情的最佳方法是什么?
谢谢.
我试图使用CRTPed基地来保存一些静态初始化代码,如下所示:
template <typename T>
class InitCRTP
{
public:
static InitHelper<T> init;
};
template <typename T> InitHelper<T> InitCRTP<T>::init;
Run Code Online (Sandbox Code Playgroud)
现在,任何需要完成工作的类InitHelper<T>都可以这样做:
class WantInit : public InitCRTP<WantInit>
{
public:
void dummy(){init;}//To force instantiation of init
};
template class InitCRTP<WantInit>;//Forcing instantiation of init through explicit instantiation of `InitCRTP<WantInit>`.
Run Code Online (Sandbox Code Playgroud)
为了强制实例化InitCRTP<WantInit>::init,我可以使用dummy或使用如上所示的显式实例化.有没有办法解决这个问题,两者都没有?我希望这种模式的用户能够简单地继承InitCRTP<WantInit>而不用担心其他任何事情.如果它有帮助,使用C++11不是问题.
我在使用boost :: tuple部分专门化模板时遇到了错误.使用std :: tuple替换boost :: tuple时编译的代码相同.这是压缩到无法编译的部分的代码.
template <typename... Args>
class Test;
template <typename... Args>
class Test<std::tuple<Args...>, Args...>
{
};
template <typename... Args>
class Test<boost::tuple<Args...>, Args...>
{
};
int main()
{
int rc;
cout<<abi::__cxa_demangle(typeid(Test<boost::tuple<int, int>, int,int>).name(), 0, 0, &rc)<<endl;//Doesn't compile
cout<<abi::__cxa_demangle(typeid(Test<std::tuple<int, int>, int,int>).name(), 0, 0, &rc)<<endl;//Compiles
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译错误,使用g ++ 48,是
tuplerr.cpp: In function ‘int main()’:
tuplerr.cpp:30:73: error: invalid use of incomplete type ‘class Test<boost::tuples::tuple<int, int, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>, int, int>’
cout<<abi::__cxa_demangle(typeid(Test<boost::tuple<int, int>, int,int>).name(), …Run Code Online (Sandbox Code Playgroud) Readline手册在此处描述了可用的条件构造。特别是手册说
应用程序构造用于包括特定于应用程序的设置。使用Readline库的每个程序都会设置应用程序名称,您可以测试特定的值。这可用于将键序列绑定到对特定程序有用的功能。例如,以下命令添加了一个键序列,该键序列引用Bash中的当前单词或上一个单词:
$if Bash
# Quote the current or previous word "\C-xq": "\eb\"\ef\""
$endif
给定使用readline库的应用程序,如何找出它为“应用程序名称”设置的值?
你如何创建类型副本?例如,如何创建类型Mass,Acceleration以及Force哪些不是隐式转换为double(或任何其他数字类型),但除此之外的所有特征double.这将允许此函数的编译时输入有效性检查:
Force GetForceNeeded(Mass m, Acceleration a);
Run Code Online (Sandbox Code Playgroud)
确保GetForceNeeded只能使用类型Mass和参数调用Acceleration.
当然,我可以通过手动创建类型的副本来实现这一点:
class Force final
{
public:
//overload all operators
private:
double value;
};
Run Code Online (Sandbox Code Playgroud)
但这很麻烦.有通用的解决方案吗?
在我的代码中,我实现了Vector3的模板,如下所示:
template <typename T>
struct TVector3{
TVector3(const T& x, const T& y, const T& z); // normal constructor from x,y,z
TVector3(const T& val); // construct from a constant value
// .. other implementation
T x, y, z;
};
// and my overload operator+ for TVector3
template <typename T>
const TVector3<T> operator+(const TVector3<T>& lhs, const TVector3<T>& rhs)
{
return TVector3<T>(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
}
Run Code Online (Sandbox Code Playgroud)
我期待这样的调用: (1,2,3)+ 1 =(2,3,4),所以我这样写了:
TVector3<float> v(1, 2, 3);
v + 1.0f; …Run Code Online (Sandbox Code Playgroud)