相关疑难解决方法(0)

sizeof(bool)是否在C++语言标准中定义?

我在标准文档中找不到答案.C++语言标准是否要求sizeof(bool)始终为1(1个字节),还是要定义此大小的实现?

c++ boolean sizeof implementation-defined-behavior

107
推荐指数
3
解决办法
6万
查看次数

是否可以使用SFINAE /模板检查操作员是否存在?

我正在尝试检查运算符是否在编译时存在,如果不存在我只是想忽略它,有没有办法做到这一点?

示例运算符:

 template <typename T>
 QDataStream& operator<<(QDataStream& s, const QList<T>& l);
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae

13
推荐指数
2
解决办法
3944
查看次数

int有一个运算符==

我正在尝试检查一个类是否有方法operator==.我在这里找到了一个SFINAE的解决方案,它与我制作的课程一起工作正常.

它看起来像这样:

template <typename T>
class comparable
{
    typedef char one;
    typedef long two;

    template <typename C> static one test( typeof(&C::operator==) ) ;
    template <typename C> static two test(...);


public:
    enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试:

std::cout << comparable<int>::value << std::endl;
Run Code Online (Sandbox Code Playgroud)

然后它返回false,而我期望它返回true.为什么是这样 ?

c++ sfinae

12
推荐指数
2
解决办法
1020
查看次数

使用SFINAE检查全局运算符<<?

我想有几个重载的全局to_string()函数,它们采用某种类型T并将其转换为字符串表示形式.对于一般情况,我希望能够写:

template<typename T,class OutputStringType> inline
typename enable_if<!std::is_pointer<T>::value
                && has_insertion_operator<T>::value,
                   void>::type
to_string( T const &t, OutputStringType *out ) {
  std::ostringstream o;
  o << t;
  *out = o.str();
}
Run Code Online (Sandbox Code Playgroud)

has_insertion_operator到目前为止,我的实施是:

struct sfinae_base {
  typedef char yes[1];
  typedef char no[2];
};

template<typename T>
struct has_insertion_operator : sfinae_base {
  template<typename U> static yes& test( U& );
  template<typename U> static no& test(...);

  static std::ostream &s;
  static T const &t;

  static bool const value = sizeof( test( s << t ) …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae

11
推荐指数
1
解决办法
2900
查看次数

仅将参数作为参数,在C++中定义operator +

如何在C++中定义operator +的函数对象(以及类型,如int,double或float)作为参数?

我几乎绝对相信这个技巧应该在模板的帮助下以某种方式完成,但由于我是C++的新手,我自己无法想象.

这种函数声明的例子非常好.

c++ templates function parameter-passing

8
推荐指数
2
解决办法
167
查看次数

模板特征类中的枚举和静态const成员变量用法

我想ostream&通过查看是否提供了重载来测试类是否可以流式传输operator<<.根据这些 帖子,我尝试使用C++ 11编写另一个版本.这是我的尝试:

#include <iostream>
#include <type_traits>

namespace TEST{
  class NotDefined{};

  template<typename T> 
  NotDefined& operator << (::std::ostream&, const T&);

  template <typename T>
  struct StreamInsertionExists {
    static std::ostream &s;
    static T const &t;
    enum { value = std::is_same<decltype(s << t), NotDefined>() };
  };
}

struct A{
  int val;
    friend ::std::ostream& operator<<(::std::ostream&, const A&);
};

::std::ostream& operator<<(::std::ostream& os, const A& a)
{
  os << a.val;
  return os;
}

struct B{};

int main() {
  std::cout << TEST::StreamInsertionExists<A>::value …
Run Code Online (Sandbox Code Playgroud)

c++ templates overload-resolution c++11

5
推荐指数
1
解决办法
472
查看次数

C++:Enforcing模板类型实现了一个方法

我有一个模板类,我想知道是否可以强制模板类类型实现某个接口,特别是我想强制该类型重载该operator=方法.在Java中我会写:

public class Tree<T implements IComparable>{
    public Tree(Vector<T> x){...}
}
Run Code Online (Sandbox Code Playgroud)

C++的替代品是什么?

c++ templates interface

4
推荐指数
1
解决办法
1861
查看次数

检测操作员+

我正在尝试使用以下代码检测运算符加上一个不与sfinae合作的代码,任何专家对我所缺少的内容都有任何想法.

当您在尝试检测它的类型上删除操作符+时,编译器也会死亡

    template<class T1, class T2>
        class has_addition_operator
        {
        private:

            typedef char no;

            static auto has(T1* a, T2* b) -> decltype( *a + *b);

            static char has(...);

        public:
            enum{
                value = (sizeof( has(new T1(), new T2())) != sizeof(no))
            };

        };


struct point{
        int x, y;

        point operator + (point const & o){

            point r = *this;
            r.x += o.x;
            r.y += o.y;
            return r;

        }

    };

    bool has = liboperator::has_addition_operator<point,point>::value;
Run Code Online (Sandbox Code Playgroud)

编译器具有以下输出:

1>------ Build started: Project: liboperator, Configuration: Debug Win32 ------ …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae c++11

4
推荐指数
1
解决办法
549
查看次数

C++ 11 std相当于Boost has_dereference

很多Boost的SFINAE助手都出现在带有C++ 11的std库中,但has_dereference似乎没有.除了这个功能,我已经设法从我的包中消除了Boost依赖,我想完全摆脱它,那么如何使用C++ 11 std功能获得相同的效果呢?

c++ boost sfinae type-traits c++11

3
推荐指数
1
解决办法
290
查看次数

如何让C++知道运算符已经为类重载了

是否可以编写特定类型的类具有某些功能或重载运算符的模板函数?例如

template <typename T>
void dosomething(const T& x){
    std::cout << x[0] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

在这个上下文中,我假设x是一个行为类似于数组的类,也就是说,我已经重载了[]运算符以及<<它可以使用它std::cout.

我的实际代码略有不同,但gcc给了我

error: subscripted value is neither array nor pointer
Run Code Online (Sandbox Code Playgroud)

这一定是因为它不知道我期望T某些类重载[]运算符.有谁知道是否有可能克服这个问题?我想让c ++知道特定类型T会有[]重载.

c++

2
推荐指数
1
解决办法
210
查看次数

在c ++ 11中检查运算符是否存在的最佳方法

我需要检查给定的类是否<<(cls, ostream)定义了运算符.如果是这样,我希望我的函数使用它来写入ostringstream,否则应该使用样板代码.

我知道之前已经问过这个问题.但是,我通常会发现自定义解决方案并不总是适用于我的编译器(clang ++).经过几个小时的搜索,我终于发现了boost :: type_traits.我之前没有看过那里,因为我假设c ++ 11已经复制了特征部门的所有内容.

对我有用的解决方案是:

template <typename C>
std::string toString(C &instance) {
    std::ostringstream out;
    out << to_string<C, boost::has_left_shift<C, std::ostream>::value>::convert(ctx);
    return out.str();
}
Run Code Online (Sandbox Code Playgroud)

to_string定义为:

template <typename C, bool>
struct to_string {
    // will never get called
    static std::string convert(LuaContext &ctx) {}
};

template <typename C>
struct to_string<C, true> {
    static std::string convert(LuaContext &ctx) {
        return "convert(true) called.";
    }
};

template <typename C>
struct to_string<C, false> {
    static std::string convert(LuaContext &ctx) { …
Run Code Online (Sandbox Code Playgroud)

c++ operators type-traits c++11

2
推荐指数
1
解决办法
1719
查看次数

有没有一种方法测试类型在编译时有"bool operator ==(const type&,const type&)"?

我编写了一个类来测试"type == type"但是当类型没有operator ==时失败了;

template <typename _Type>
double _test(...){
    return 0;
}

template <typename _Type>
auto _test(_Type&& t)->decltype(t == t){
    return 0;
}

template <typename _Type>
struct has_equal_to
{
    static const bool value = sizeof(_test(std::declval<_Type>())) == sizeof(char);
};

struct test{};

int main()
{
    std::cout << has_equal_to<test>::value << std::endl; // compile failed ~~~~
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?或者写一个这样的课是不可能的.....

c++

2
推荐指数
1
解决办法
136
查看次数