例如
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++编程语言"第四版 - 第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; …
我试图覆盖一个 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) 我正在尝试编写自己的向量模板类,但是在编写友元函数声明时遇到了一些问题。
一开始我是这样写的:
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>的友元函数。
在模板类中编写友元函数的正确方法是什么?
我有一个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构造函数中不起作用.
我不知道为什么会这样.
考虑这段代码
#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) 我定义了一个模板类 ( DataArray<T>),并且想要定义一个函数来计算整型数组 ( 、、... ) 或复杂类型数组 ( 、 、 ... )min()的最小值。doublefloatintstd::complex<double>std::complex<float>
我正在尝试使用类型特征来选择正确的函数。尽管这里的讨论非常精彩,但我的代码无法编译:
Run Code Online (Sandbox Code Playgroud)DataArray<double> and DataArray<std::complex<double>>: no matching overloaded function
问题是什么?这是我的代码的最小部分:
#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) 当我在写一个带有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) 在下面的代码中编写语句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) 来自 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++ ×10
templates ×4
c++11 ×3
definition ×2
friend ×2
c++17 ×1
constants ×1
constexpr ×1
dictionary ×1
iterator ×1
overriding ×1
signature ×1
statements ×1
stl ×1
syntax ×1
type-alias ×1
type-traits ×1
using ×1
vector ×1
warnings ×1