因此,从C++ 14开始,constexpr
C++ 11中的限制已经消失,例如在constexpr
函数中有新的变量或循环.
最新版本的GCC和Clang编译器已经支持它们.
所以问题是这个...... constexpr
函数是在编译期间而不是在执行期间计算的,只要作为参数传递给它的值是常量.所以我在下面写的函数的结果应该在执行期间瞬间出现,对吧?但事实并非如此.
我的问题是:为什么会这样?我对C++ 14的constexpr
功能有错误的理解吗?谢谢.
编辑:是的,我正在使用-OO
,这就是为什么它不起作用.但是设置-O1
或更高速度优化可以解决问题并且程序按预期执行.谢谢大家的答案.
#include <iostream>
#include <chrono>
constexpr long long addition(long long num)
{
long long sum = 0;
for (int i = 0; i <= num; i++)
{
sum += i;
}
return sum;
}
int main()
{
auto start = std::chrono::steady_clock::now();
//////////////////////////////////////////////
std::cout << addition(500000000); //500 mill //executes in 1.957 seconds
///////////////////////////////////////////////
auto stop = std::chrono::steady_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(stop - …
Run Code Online (Sandbox Code Playgroud) 我有三个类的层次结构,Derived
派生自Selectable
和Drawable
.然后,我有一个std::vector
的std::unique_ptr<Drawable>
,我充满了Derived
对象.
我确信矢量将仅由同时来自两个碱基的物体填充.
当我尝试通过使用指针从向量中删除某个元素时,问题就出现了Selected
.
#include <vector>
#include <memory>
#include <algorithm>
struct Selectable {
virtual ~Selectable() = 0;
};
Selectable::~Selectable() = default;
struct Drawable {
virtual ~Drawable() = 0;
};
Drawable::~Drawable() = default;
struct Derived : Selectable, Drawable {};
int main()
{
std::vector<std::unique_ptr<Drawable>> vec;
for (int i = 0; i < 5; ++i) {
vec.push_back(std::make_unique<Derived>());
}
Selectable* selected = dynamic_cast<Selectable*>(vec[2].get());
vec.erase(std::remove_if(vec.begin(), vec.end(),
[selected](auto&& ptr) {
return …
Run Code Online (Sandbox Code Playgroud) 假设我有以下Less设置:
.box {
border: 1px solid #333;
&.error {
background-color: Red;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我想声明另一个应用完整样式的.box.error的类,.error-box
例如,正确的语法是什么?
如果我使用:
.error-box {
.box.error;
}
Run Code Online (Sandbox Code Playgroud)
我得到的只是红色背景,没有边框.我尝试了很多不同的组合,但我总是遇到语法错误.
我们得到一个图表,其中包含以下事实:
edge(a,b)
edge(a,c)
edge(b,a)
edge(c,d)
edge(d,d)
edge(d,e)
edge(e,f)
edge(f,g)
edge(g,e)
Run Code Online (Sandbox Code Playgroud)
我们被要求定义一个规则,cycle(X)
确定是否存在从节点开始的循环X
.
我真的迷失了如何做到这一点,我试图遍历节点并检查下一个节点是否会再次成为起始节点,但我似乎无法让它工作
所以我们假设我们有以下代码:
#include <stack>
template<class... Args>
auto make_stack(Args&&... args)
{
std::stack<INSERT_TYPE_HERE> s;
return s;
}
int main()
{
auto s = make_stack(1, 2.2, 3); //would be std::stack<double>
auto s2 = make_stack(1l, 2, 3); //would be std::stack<long>
}
Run Code Online (Sandbox Code Playgroud)
如何在参数包中找到常见的参数类型?
所以,我正在关注此网页上代码所设置的示例:http: //eli.thegreenplace.net/2014/sfinae-and-enable_if/
这就是我所拥有的:
template<typename T>
void fun(const typename std::enable_if_t<std::is_integral<T>::value, T>& val) {
std::cout << "fun<int>";
}
template<typename T>
void fun(const typename std::enable_if_t<std::is_floating_point<T>::value, T>& val) {
std::cout << "fun<float>";
}
int main()
{
fun(4);
fun(4.4);
}
Run Code Online (Sandbox Code Playgroud)
这样我就得写:
fun<int>(4);
fun<double>(4.4);
Run Code Online (Sandbox Code Playgroud)
我怎么能避免这种情况?
编译器抱怨它无法推断出参数T
.
问题很简单.
在32位系统上:
std::cout << sizeof(unsigned int); //4
std::cout << sizeof(unsigned long long); //8
std::cout << sizeof(std::size_t); //4
Run Code Online (Sandbox Code Playgroud)
在64位系统上:
std::cout << sizeof(unsigned int); //4
std::cout << sizeof(unsigned long long); //8
std::cout << sizeof(std::size_t); //8
Run Code Online (Sandbox Code Playgroud)
我只检查了MSVC的实现,它看起来像这样:
#ifdef _WIN64
typedef unsigned __int64 size_t;
#else
typedef unsigned int size_t;
#endif
Run Code Online (Sandbox Code Playgroud)
那么为什么不在32位和64位系统上make std::size_t
unsigned long long
(std::uintmax_t
),当它们明确支持它时?或者我错了吗?
这么简单的问题.
template<class InputIt, class Size, class OutputIt>
OutputIt copy_n(InputIt first, Size count, OutputIt result);
Run Code Online (Sandbox Code Playgroud)
为什么std::copy_n
要为要复制的元素数量而不是简单地采用一种类型std::size_t
?我只是想不出一个理由.
template<class InputIt, class OutputIt>
OutputIt copy_n(InputIt first, std::size_t count, OutputIt result);
Run Code Online (Sandbox Code Playgroud) 我对技术物流很感兴趣.是否有任何优势,例如保存的内存等,来实现处理类的某些功能?
特别是,将操作符重载实现为自由函数(假设您不需要访问任何私有成员,即使这样,您也可以让他们使用非成员的朋友)?
每次创建对象时,是否为类的每个函数提供了不同的内存地址?
制作仅移动类型的地图向量似乎在 Windows 上无法正常工作。请参阅此处的代码: https: //godbolt.org/z/yAHmzh
#include <vector>
#include <map>
#include <memory>
// vector<vector<move-only>> works
void foo() {
std::vector<std::vector<std::unique_ptr<int>>> outer;
std::vector<std::unique_ptr<int>> inner;
std::unique_ptr<int> p = std::make_unique<int>(1);
inner.push_back(std::move(p));
outer.push_back(std::move(inner));
}
// vector<map<move-only>> fails to compile upon inserting an element.
void bar() {
std::vector<std::map<std::unique_ptr<int>, std::unique_ptr<int>>> vec;
std::map<std::unique_ptr<int>, std::unique_ptr<int>> map;
std::unique_ptr<int> p1 = std::make_unique<int>(1);
std::unique_ptr<int> p2 = std::make_unique<int>(2);
map.insert(std::make_pair(std::move(p1), std::move(p2)));
// The following line fails to compile on windows. It errors with a message about
// the unique_ptr copy constructor being explicitly deleted. This …
Run Code Online (Sandbox Code Playgroud) c++ ×8
c++11 ×5
c++14 ×2
templates ×2
class ×1
constexpr ×1
less ×1
optimization ×1
polymorphism ×1
prolog ×1
sfinae ×1
vector ×1
visual-c++ ×1