小编son*_*yao的帖子

如何为类中的模板定义类型别名

例如

struct Option_1
{
    template<class T> using Vector = std::vector<T>;
};
Run Code Online (Sandbox Code Playgroud)

我可以

typename Option_1::Vector<int> v;
Run Code Online (Sandbox Code Playgroud)

但我更喜欢下面的

Vector<Option_1, int> v;
Run Code Online (Sandbox Code Playgroud)

或不带“typename”一词的类似名称。我定义了一个别名

template<class Option, class T> using Vector= typename Option::Vector<T>;
Run Code Online (Sandbox Code Playgroud)

但由于无法识别的模板声明/定义而失败。如何修复它?

c++ templates using c++11 type-alias

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

正确的朋友模板功能的语法

在"C++编程语言"第四版 - 第23.4.7章"朋友"中,我找到了以下示例(我稍微修改了它以仅显示相关部分):

template<typename T>
class Vector {
public:
    friend Vector operator*<>(const Vector& v, int f); 
                           ^^ ~~~~ ?
};

template<typename T>
Vector<T> operator*(const Vector<T>& v, int f) {
    return v;
}
Run Code Online (Sandbox Code Playgroud)

我试图编译它,但我得到以下错误(clang):

main.cpp:8:20: error: friends can only be classes or functions
        friend Vector operator*<>(const Vector& v, int f); 
                      ^
main.cpp:8:29: error: expected ';' at end of declaration list
        friend Vector operator*<>(const Vector& v, int f); 
                               ^
                               ;
2 errors generated.
Run Code Online (Sandbox Code Playgroud)

书解释说:

需要友元函数名称后面的<>来表明朋友是模板函数.如果没有<>,则假定为非模板函数.

这就是全部.

如果没有<>此代码编译,但在使用operator*时(例如Vector<int> v; v*12; …

c++ templates friend

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

带有尾随返回类型的 final、override、const 的语法

我试图覆盖一个 virtual 但也使用关键字override, finaland const,带有尾随返回类型。问题似乎出在派生类上,编译器错误(说我没有指定尾随返回类型)并没有太大帮助。代码在这里:https : //wandbox.org/permlink/zh3hD4Ukgrg6txyE

还贴在下面。我玩过不同的排序,但似乎仍然无法正确。任何帮助将不胜感激,谢谢。

#include<iostream>
using std::cout; using std::endl; using std::ostream;
//////////////////////////////////////////////
//Base stuff
class Base
{
public:
  Base(int i=2):bval(i){}
  virtual ~Base()=default;
  virtual auto debug(ostream& os=cout)const->ostream&;

private:
  int bval=0;
};

auto Base::debug(ostream& os) const->ostream&
{
  os << "bval: " << bval << endl;
  return os;
}

///////////////////////////////////////////////
//Derived stuff
class Derived : public Base
{
public:
  Derived(int i=2,int j=3):Base(i), dval(j){}
  ~Derived()=default;

  auto debug(ostream& os=cout) const override final->ostream&; // error here

private: …
Run Code Online (Sandbox Code Playgroud)

c++ syntax c++11

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

在模板类中编写友元函数声明的正确方法是什么?

我正在尝试编写自己的向量模板类,但是在编写友元函数声明时遇到了一些问题。

一开始我是这样写的:

template <typename T, typename Alloc = std::allocator<T>>
class vector {
public:
    friend bool operator==(const vector<T, Alloc>&, const vector<T, Alloc>&);
};
Run Code Online (Sandbox Code Playgroud)

但是编译器报告了一个警告,我声明了一个非模板函数。所以我把朋友声明改成了这样:

template <typename T, typename Alloc = std::allocator<T>>
class vector {
public:
    template <typename E, typename F>
    friend bool operator==(const vector<E, F>&, const vector<E, F>&);
};
Run Code Online (Sandbox Code Playgroud)

到目前为止一切都很好,但我认为仍然存在问题。如果我这样写,我会创建所有operator==将两个模板参数作为其友元函数的函数。例如,operator==(const vector<int>&, const vector<int>&)andoperator==(const vector<double>&, const vector<double>&)都是vector<int>的友元函数。

在模板类中编写友元函数的正确方法是什么?

c++ templates vector friend

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

对shared_ptr中的const int的未定义引用

我有一个Config课程

// config.hpp

class Config {
  public:
    static constexpr int a = 1;
    static constexpr int b = 1;
}
Run Code Online (Sandbox Code Playgroud)

并包含在main.cpp中

// main.cpp
#include "config.hpp"
int main () {
  std::cout << Config::a << std::endl; // this is ok
  std::shared_ptr<otherClass> stream = std::make_shared<otherClass>( 
Config::a); // compile error
}
Run Code Online (Sandbox Code Playgroud)

和编译器说 undefined reference to Config::a

它在使用时起作用cout,但在shared_ptr构造函数中不起作用.

我不知道为什么会这样.

c++ definition one-definition-rule constexpr c++17

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

在解除引用map迭代器时返回对临时的引用

考虑这段代码

#include <iterator>
#include <vector>

const int& foo(const std::vector<int>& x,unsigned i) {
    auto it = x.begin();
    std::advance(it,i);
    return *it;
}
Run Code Online (Sandbox Code Playgroud)

clang和gcc都没有发出错误/警告,但是:

#include <iterator>
#include <map>

const std::pair<int,int>& bar(const std::map<int,int>& x,unsigned i){
    auto it = x.begin();
    std::advance(it,i);
    return *it;
}
Run Code Online (Sandbox Code Playgroud)

clang编译并使用-Werror结果:

<source>:14:12: error: returning reference to local temporary object [-Werror,-Wreturn-stack-address]
    return *it;
           ^~~
Run Code Online (Sandbox Code Playgroud)

gcc:

<source>: In function 'const std::pair<int, int>& bar(const std::map<int, int>&, unsigned int)':
<source>:14:13: error: returning reference to temporary [-Werror=return-local-addr]
     return *it;
             ^~ …
Run Code Online (Sandbox Code Playgroud)

c++ warnings dictionary iterator

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

如何编写类型特征方法

我定义了一个模板类 ( DataArray<T>),并且想要定义一个函数来计算整型数组 ( 、、... ) 或复杂类型数组 ( 、 、 ... )min()的最小值。doublefloatintstd::complex<double>std::complex<float>

我正在尝试使用类型特征来选择正确的函数。尽管这里的讨论非常精彩,但我的代码无法编译:

DataArray<double> and DataArray<std::complex<double>>: no matching overloaded function
Run Code Online (Sandbox Code Playgroud)

问题是什么?这是我的代码的最小部分:

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

template <typename T>
class DataArray {
public:
    DataArray(T * data) : m_data(data) {}

    template<typename T>
    using isComplex = std::is_same<T, std::complex<typename T::value_type>>;

    template <typename T>
    typename std::enable_if<isComplex<T>::value>::type min() {
        std::cout << "Min for complex" << std::endl;
    }

    template <typename T>
    typename std::enable_if<std::is_arithmetic<T>::value>::type …
Run Code Online (Sandbox Code Playgroud)

c++ templates type-traits

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

c ++用const参数而不是非常量参数覆盖虚函数

当我在写一个带有const参数而不是非常量参数的overide函数时,我以为编译器会报错,因为base函数有非常量参数,但是编译成功了。为什么?

我的代码:

#include <iostream>

class A
{
public:
    virtual uint64_t cal(uint64_t value)
    {
        std::cout << value << std::endl;
        return value;
    }
};
class B : public A
{
public:
    uint64_t cal(const uint64_t value) override;
};
uint64_t B::cal(const uint64_t value)
{
  std::cout << value + 1 << std::endl; 
  return (value+1);
}
int main()
{
    B b;
    b.cal(1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ overriding constants signature

5
推荐指数
2
解决办法
330
查看次数

在 C++ 中使用命名空间时出现“未命名类型”错误

在下面的代码中编写语句A::x=5给出了错误:

命名空间“A”中的“x”未命名类型

我们不能为x变量全局赋值吗?

#include <iostream>

int x = 10;  

namespace A
{
    int x = 20; 
}

A::x=5;

int main()
{
    int x = 30; 
    std::cout << "x = " << x << std::endl;
    std::cout << "A::x = " << A::x << std::endl;
    std::cout << "::x = " << ::x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ definition statements

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

为什么 std::pair 对 const 引用和转发引用参数有两个不同的构造函数?

来自 ISO 标准(准确地说是 N4860)的std::pair概要:

constexpr explicit(see below) pair(const T1& x, const T2& y); // first constructor

template<class U1, class U2>
constexpr explicit(see below) pair(U1&& x, U2&& y); // second constructor
Run Code Online (Sandbox Code Playgroud)

我似乎找不到任何理由为什么第一个构造函数应该与完美转发构造函数一起定义。完美的转发构造函数是否足以处理复制、移动两种情况?在哪种情况下,第一个构造函数在重载决议中获胜?

c++ stl language-lawyer c++11 forwarding-reference

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