小编Lor*_*ins的帖子

C++标准库中是否有纯虚函数?

讲座中,发言者提到(一开始)标准库中没有纯虚函数(或者他没有注意到).我相信Alex Stepanov反对这种语言功能但是从最初的STL设计开始,是否有任何纯粹的虚拟内容进入标准库?

FWIW(并纠正我,如果我错了)唯一指针中的删除器最终在大多数实现中使用虚拟调度,但这些不是纯虚拟.

c++ virtual-functions pure-virtual c++-standard-library language-lawyer

32
推荐指数
2
解决办法
1776
查看次数

这是转发参考吗?

在这个例子中,Scott Meyers明确了rvalue引用和转发引用之间的区别:

Widget&& var1 = someWidget;     // here, “&&” means rvalue reference (1)

auto&& var2 = var1;             // here, “&&” does not mean rvalue reference (2)

template<typename T>
void f(std::vector<T>&& param); // here, “&&” means rvalue reference (3)

template<typename T>
void f(T&& param);              // here, “&&”does not mean rvalue reference (4)
Run Code Online (Sandbox Code Playgroud)

本质上,当我们有一个可推导的上下文时就会出现这种区别,因此案例(3)明确表明我们有一个,vector<...>&&T案例(4)是要推断的,并且(在应用参考折叠规则之后)按"价值类别"分类.

但是更复杂的模式匹配会发生什么?以下面的情况为例:

template <template <class...> class Tuple, class... Ts>
void f(Tuple<Ts...>&& arg)
{

}
Run Code Online (Sandbox Code Playgroud)

&&这里的意思是什么?

c++ perfect-forwarding c++11 forwarding-reference

20
推荐指数
2
解决办法
2994
查看次数

如何不优化远离 - 愚蠢功能的机制

我正在寻找一种编程技术,确保用于基准测试的变量(没有可观察到的副作用)不会被编译器优化掉

提供了一些信息,但我最终使用了愚蠢和以下功能

/**
 * Call doNotOptimizeAway(var) against variables that you use for
 * benchmarking but otherwise are useless. The compiler tends to do a
 * good job at eliminating unused variables, and this function fools
 * it into thinking var is in fact needed.
 */
#ifdef _MSC_VER

#pragma optimize("", off)

template <class T>
void doNotOptimizeAway(T&& datum) {
  datum = datum;
}

#pragma optimize("", on)

#else
template <class T>
void doNotOptimizeAway(T&& datum) {
  asm volatile("" : "+r" (datum)); …
Run Code Online (Sandbox Code Playgroud)

c++ benchmarking assembly c++11 c++14

19
推荐指数
1
解决办法
1853
查看次数

复合文字是标准C++吗?

复合文字是C99结构.即使我可以用C++做到这一点:

#include <iostream>
using namespace std;

int main() {
    for (auto i : (float[2]) {2.7, 3.1}) cout << i << endl;
}
Run Code Online (Sandbox Code Playgroud)

似乎MSVC支持它作为扩展.然而,我可以得到所有编译器,编译上面提到的代码.

是C++ 14中的一个功能吗?是否有一个不同的标准术语(在我看来,只是创建一个临时使用支撑初始化)?


旁注:"复合文字"(或者我应该称之为的任何内容)是包扩展上下文(仅提及功能)

c++ compound-literals c++14

18
推荐指数
2
解决办法
6633
查看次数

模板参数包中的6个点是什么?

在看这个问题的时候,我发现自己在cpp参考站点,我发现了一个奇怪的新手语法:

template<class Ret, class... Args>
struct is_function<Ret(Args......)volatile &&> : std::true_type {};
Run Code Online (Sandbox Code Playgroud)

是的,6个点!最初我认为这是一个拼写错误,但在再次检查libstdc ++ 之后,例如在第444行:

template<typename _Res, typename... _ArgTypes>
struct is_function<_Res(_ArgTypes......) volatile &&> : public true_type { };
Run Code Online (Sandbox Code Playgroud)

这是一个有效的语法吗?点点,用于打包和解包参数包?6个点做什么?

c++ partial-specialization variadic-templates c++11 c++14

15
推荐指数
2
解决办法
2912
查看次数

在这种情况下,在cpp文件中编写模板特化是否可以?

假设我的代码以这种方式构建:

  • 那么header1.h

    template <class T, template<class> class C>
    struct metafunction {
        using type = typename C<T>::type; 
    };
    
    inline namespace msn {
        template <class T> struct implementation; 
    }
    
    // uses the *implementation* not defined in the header!
    template <class T>
    struct use_case {
        using type = typename metafunction<T, implementation>::type; 
    }; 
    
    Run Code Online (Sandbox Code Playgroud)
  • cpp1.cpp

    #include <header1.h>
    
    // I'll only need this in this compilation unit, so the
    // question is:    "Is this a good place to define it?"
    template <>
    struct implementation<int> {
        using …
    Run Code Online (Sandbox Code Playgroud)

c++ template-specialization

15
推荐指数
1
解决办法
993
查看次数

我如何使用两个Anaconda版本(2.7和3.5)?

我在Windows 10机器上使用Anaconda 3.5 发行版.由于我想要使用的库中的依赖项,我必须2.7安装该版本.

好消息是我需要的库现在可以2.7顺利地使用该版本,Visual Studio 2015可以自动检测我的新Python环境.

使用命令行时出现问题.发出命令后

conda info --envs
Run Code Online (Sandbox Code Playgroud)

我明白了

root                  *  C:\Users\norah\Anaconda2
Run Code Online (Sandbox Code Playgroud)

即在单一环境下(我的理解和搜索,到目前为止,根据这个我应该看到两个ENVS列出).这意味着我不能用于conda获取我的Python3.5安装的新软件包,至少不能像以前那样在命令行中conda获取Python2.7.GUI版本,Anaconda导航器也是如此(我不是非常喜欢GUI版本,但我尝试了它).

从那以后,也无法从命令行启动python3

$python
Run Code Online (Sandbox Code Playgroud)

总是激活python2.7并且在命令行中发出$python3或者$python3.5似乎不起作用(也不会将python3的路径添加到系统中,因为实际的可执行文件与python2具有相同的名称ie python.exe)

我的系统是由Python2.7接管的吗?有没有人在这里顺利使用它们,如果可以,请你详细说明如何做到这一点?安装两个版本的Anaconda是一个"不是不"的举动吗?

python python-2.7 python-3.x anaconda

14
推荐指数
2
解决办法
3万
查看次数

返回转发参考参数 - 最佳实践

在以下场景中

template <class T>
? f(T&& a, T&& b)
{
    return a > b ? a : b; 
}
Run Code Online (Sandbox Code Playgroud)

最佳回报类型是什么?到目前为止我的想法是:

  1. 返回r值refs,完美转发函数参数:

    template <class T>
    decltype(auto) f(T&& a, T&& b)
    {
        return a > b ? forward<T>(a) : forward<T>(b); 
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 移动构造返回值:

    template <class T>
    auto f(T&& a, T&& b)
    {
        return a > b ? forward<T>(a) : forward<T>(b); 
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 尝试找到一种方法来启用(N)RVO(即使我认为因为我想使用不可能的函数参数)

这些解决方案有问题吗?还有更好的吗?什么是最佳做法?

c++ rvalue-reference move-semantics c++11 c++14

11
推荐指数
2
解决办法
747
查看次数

为什么非下划线名称保留给UDL的实现而不是相反?

我知道这是一个古老的功能,但阅读用户定义的文字,例如

return_t operator "" _a(long); // Literal operator for user-defined INTEGRAL literal
Run Code Online (Sandbox Code Playgroud)

我被提醒了

领先的下划线是必需的.(只允许标准库定义没有下划线的文字.)(A)

这似乎打破了模式,因为到目前为止,实现与下划线名称一起使用并留给我们好的,例如保留给实现的名称

  1. 包含双下划线
  2. 首先是下划线,后跟大写字母

此外,在存在规则(2)的情况下,引用(A)有些危险.那么为什么不一致呢?

c++ user-defined-literals

10
推荐指数
1
解决办法
204
查看次数

打印带有n位小数的整数或浮点数

在Python中,如果后一种情况要求我将打印输出限制为一定数量的数字,如何打印可能是整数或实数的数字?

长话短说,我们有以下例子:

print("{0:.3f}".format(num)) # I cannot do print("{}".format(num))
                             # because I don't want all the decimals
Run Code Online (Sandbox Code Playgroud)

是否存在"Pythy"方式以确保例如,如果num == 1我打印1而不是1.000(我的意思是除了使用if语句混乱我的代码)

python floating-point precision int python-3.x

9
推荐指数
2
解决办法
1912
查看次数