当使用using继承基类的构造函数时,英特尔C++编译器(版本16.0.3.207 Build 20160415)似乎删除了显式说明符.这是一个错误吗?
struct B
{
explicit B(int) { }
};
struct D : B
{
using B::B;
};
B b = 1; // Not OK, fine
D d = 1; // Not OK with Microsoft C++ and GCC, but OK with Intel C++
Run Code Online (Sandbox Code Playgroud) 铛(7,8,主干)拒绝以下代码
enum class E {};
inline static constexpr auto e = E{};
// inline static constexpr auto e = nullptr;
template<auto, int> class S;
template<int a, int b> class S<a, b> {};
template<int b> class S<e, b> {};
int main() {
S<0, 0> s;
}
Run Code Online (Sandbox Code Playgroud)
带有错误:
Run Code Online (Sandbox Code Playgroud)error: ambiguous partial specializations of 'S<0, 0>' note: partial specialization matches [with a = 0, b = 0] template<int a, int b> class S<a, b> {}; ^ note: partial specialization matches [with b = …
我知道关键字在封装方面的一般用例,friend
但有几次,我需要关键字friend
只是为了“完成工作”。这些用例并不让我高兴,所以我想知道是否有一些替代方案。这是第一个最小的例子:
struct Foo{
enum class Bar{
a=1,b=2,c=4
};
// need to tell the compiler of operator| before it gets used
// but it can't be a member function of Foo: so add friend keyword
friend Bar operator|(const Bar& b1, const Bar& b2);
// constructor needs a default value using
// operator| for Bars
Foo( Bar b = Bar::a | Bar::b );
};
// definition of operator|, etc.
Run Code Online (Sandbox Code Playgroud)
在构造函数声明中给出默认值之前,编译器有什么方法可以查看接口内部嵌套类的声明吗?operator|
Foo …
c++ templates operator-overloading friend non-member-functions
我创建了一个头文件。一些简单的如下。
#pragma once
#include <iostream>
template<typename T>
void say(T t) {
std::cout << t << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
然后用于g++
创建gch
带有g++ hello.h
. 它给了我这个警告->
pch.h:2:9: warning: #pragma once in main file
2 | #pragma once
| ^~~~
Run Code Online (Sandbox Code Playgroud)
但是gch
创建的文件和预编译的头文件工作正常。如果我使用标题守卫,这个错误就会消失。
我在这里做错了吗?
#include<iostream>
#include<string>
template <typename T>
void swap(T a , T b)
{
T temp = a;
a = b;
b = temp;
}
template <typename T1>
void swap1(T1 a , T1 b)
{
T1 temp = a;
a = b;
b = temp;
}
int main()
{
int a = 10 , b = 20;
std::string first = "hi" , last = "Bye";
swap(a,b);
swap(first, last);
std::cout<<"a = "<<a<<" b = "<<b<<std::endl;
std::cout<<"first = "<<first<<" last = "<<last<<std::endl;
int c …
Run Code Online (Sandbox Code Playgroud) #include <utility>
template <typename Container>
decltype(auto) index(Container &&arr, int n) {
return std::forward<Container>(arr)[n];
}
Run Code Online (Sandbox Code Playgroud)
进行函数调用:
#include <vector>
index(std::vector {1, 2, 3, 4, 5}, 2) = 0;
Run Code Online (Sandbox Code Playgroud)
当函数调用完成时,对象std::vector {1, 2, 3, 4, 5}
将被销毁,为释放的地址赋值会导致未定义的行为。但是上面的代码运行良好,valgrind 什么也没检测到。也许编译可以帮助我制作另一个不可见的变量,例如
auto &&invisible_value {index(std::vector {1, 2, 3, 4, 5}, 2)};
invisible_value = 9;
Run Code Online (Sandbox Code Playgroud)
如果我的猜测不正确,我想知道为什么为从函数返回的右值引用赋值是可行的,以及临时对象index(std::vector {1, 2, 3, 4, 5}, 2)
何时会被销毁。
这个想法起源于?Effective Modern C++?,Item3:理解decltype
。
(假设我正在使用一个需要使用原始指针的库或框架,)
使用拥有一些数据的智能指针,然后将解除保护的智能指针的地址传递给需要原始指针的函数是否有效?
我已经学习 Haskell 大约 4 个月了,我不得不说,学习曲线绝对是艰难的(也很可怕:p)。
在解决了大约 15 个简单问题后,今天我转向 HackerRank 上的第一个中等难度问题https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem。
这是 10 个测试用例,我能够通过其中的 6 个,但其余的都因超时而失败,现在有趣的部分是,我已经可以看到一些具有性能提升潜力的部分,例如,我正在使用nub
删除复制自 a [Int]
,但我仍然无法构建算法性能的心理模型,不确定 Haskell 编译器的主要原因将改变我的代码以及懒惰在这里如何发挥作用。
import Data.List (nub)
getInputs :: [String] -> [String]
getInputs (_:r:_:p:[]) = [r, p]
findRating :: Int -> Int -> [Int] -> Int
findRating step _ [] = step
findRating step point (x:xs) = if point >= x then step else findRating (step + 1) point xs
solution :: [[Int]] -> [Int]
solution [rankings, points] = map …
Run Code Online (Sandbox Code Playgroud) 方便的initializer_list
语法似乎是以无法移动列表成员、创建不必要的副本为代价的。
struct A
{
// some members which are dynamic resources...
A() { cout << "Default Constructor\n"; }
A(const A& original) { cout << "Copy constructor\n"; }
A(A&& original) { cout << "Move constructor\n"; }
};
int main() {
vector<A> v1{ A() , A() }; // calls copy
vector<A> v2;
v2.push_back(A()); v2.push_back(A()); // calls move
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,这是因为取消引用初始化器迭代器会给出const T
,即使尝试移动时也会复制它。
有解决方法吗?
阅读/sf/answers/3101541901/,提出了一种使用可变参数模板的解决方案,如下所示:
template<class Array> struct maker;
// a maker which makes a std::vector
template<class …
Run Code Online (Sandbox Code Playgroud) 我最近反对这个没有任何评论的代码.它找到了字的最小循环移位(此代码专门返回其在字符串中的索引)和它的称为Duval算法.只有我发现的信息用很少的单词描述算法并且代码更清晰.在理解这个算法时,我将不胜感激.我总是发现文本算法相当棘手,而且很难理解.
int minLexCyc(const char *x) {
int i = 0, j = 1, k = 1, p = 1, a, b, l = strlen(x);
while(j+k <= (l<<1)) {
if ((a=x[(i+k-1)%l])>(b=x[(j+k-1)%l])) {
i=j++;
k=p=1;
} else if (a<b) {
j+=k;
k=1;
p=j-i;
} else if (a==b && k!=p) {
k++;
} else {
j+=p;
k=1;
}
}
return i;
}
Run Code Online (Sandbox Code Playgroud)