小编car*_*rlo的帖子

使用operator []时如何区分读/写操作

我需要编写一个带有重载operator []的类,当运算符[]用于读取或写入数据时,它具有不同的行为.为了给出我想要实现的实际示例,假设我必须编写一个名为PhoneBook的类的实现,该类可以通过以下方式使用:

PhoneBook phoneBook(999999); // 999999 is the default number which should be
                             // used when calling someone who is not in the phone book

phoneBook["Paul"] = 234657;  // adds Paul's number
phoneBook["John"] = 340156;  // adds John's number

// next line should print Paul's number 234657
cout << "To call Paul dial " << phoneBook["Paul"] << endl;
// next line should print John's number 340156
cout << "To call John dial " << phoneBook["John"] << endl;
// next line …
Run Code Online (Sandbox Code Playgroud)

c++

8
推荐指数
1
解决办法
972
查看次数

没有虚函数的 C++ 接口

我想知道如何在不使用虚函数的情况下在 C++ 中声明一个接口。经过一些互联网搜索,我整理了这个解决方案:

#include <type_traits>

using namespace std;

// Definition of a type trait to check if a class defines a member function "bool foo(bool)"
template<typename T, typename = void>
struct has_foo : false_type { };

template<typename T>
struct has_foo<T, typename enable_if<is_same<bool, decltype(std::declval<T>().foo(bool()))>::value, void>::type> : true_type { };

// Definition of a type trait to check if a class defines a member function "void bar()"    
template<typename T, typename = void>
struct has_bar : false_type { };

template<typename T>
struct has_bar<T, …
Run Code Online (Sandbox Code Playgroud)

c++ interface static-polymorphism

6
推荐指数
2
解决办法
3410
查看次数

我们可以使用参数包作为 std::vector 初始值设定项吗?

我正在试验 C++11(到目前为止我已经使用了旧的 C++)并且我编写了以下代码:

#include <iostream>
#include <vector>
#include <type_traits>

using namespace std;

constexpr bool all_true(){
    return true;
}

template <typename Head, typename... Tail>
constexpr bool all_true(Head head, Tail... tail){
    static_assert( is_convertible<bool, Head>::value, "all_true arguments must be convertible to bool!");
    return static_cast<bool>(head) && all_true(tail...);
}

template<typename T, typename... Args>
void print_as(Args... args){
    static_assert( all_true(is_convertible<T,Args>::value...), "all arguments must be convertible to the specified type!");
    vector<T> v {static_cast<T>(args)...};
    for(T i : v) cout << i << endl;
}

int main(){
    print_as<bool>(1, 2, 0, …
Run Code Online (Sandbox Code Playgroud)

stdvector variadic-templates c++11

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

如何从成员函数中检测实例是否为const?

我想编写一个成员函数来检测实例化对象是否为const.

举一个简单的例子,我们可以考虑以下类定义

class Foo{
  public:
    void constnessChecker(){
      bool isConst;
      // MORE CODE GOES HERE...
      if (isConst) {
        std::cout << "This instance is const! << std::endl;
      } else {
        std::cout << "This instance is not const! << std::endl;
      }
    }
};
Run Code Online (Sandbox Code Playgroud)

和以下代码

int main(){
  Foo foo1;
  Foo const foo2;
  foo1.constnessChecker();
  foo2.constnessChecker();
}
Run Code Online (Sandbox Code Playgroud)

哪个应该产生

This instance is not const!
This instance is const!
Run Code Online (Sandbox Code Playgroud)

这可能吗?

c++ const

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