小编T.C*_*.C.的帖子

'typename'和别名模板

以下代码使用Clang和GCC编译,即使Foo_t<T>::Bar它没有typename在它前面:

struct Foo {
    using Bar = int;
};

template<class...>
using Foo_t = Foo;

template<class T>
void f(){
    Foo_t<T>::Bar b; // No typename!
}

int main(){
    f<int>();
}
Run Code Online (Sandbox Code Playgroud)

它应该编译吗?

c++ templates language-lawyer c++11

24
推荐指数
2
解决办法
1365
查看次数

C++是否允许优化编译器忽略for-condition的副作用?

在调试一些遗留代码时,我偶然发现令人惊讶的(对我而言)编译器行为.现在我想知道C++规范中的任何子句是否允许以下​​优化,其中函数调用对for-condition的副作用被忽略:

void bar() 
{
   extern int upper_bound;
   upper_bound--;
}

void foo()
{
   extern int upper_bound; // from some other translation unit, initially ~ 10
   for (int i = 0; i < upper_bound; ) {
      bar();
   }
}
Run Code Online (Sandbox Code Playgroud)

在得到的解析中,存在一个控制路径,其中upper_bound保存在寄存器中,并且upper_boundin 的递减bar()永远不会生效.

我的编译器是Microsoft Visual C++ 11.00.60610.1.

老实说,我在N3242的 6.5.3和6.5.1中没有看到太多的摆动空间,但我想确定我没有错过任何明显的东西.

c++ for-loop specifications extern language-lawyer

22
推荐指数
2
解决办法
723
查看次数

奇怪的GCC数组初始化行为

在查看另一个问题时我遇到了这段代码的变体(原始代码使用的是std::thread代替std::vector,但语法是相同的):

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    std::vector<double> vecs[10] = std::vector<double>(10, 1);
    for(auto& vec: vecs){
        std::copy(vec.begin(), vec.end(), std::ostream_iterator<double>(std::cout, " "));
        std::cout<<std::endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这段代码不应该编译; std::vector<double> vecs[10] = std::vector<double>(10, 1);是无效的初始化语法,并且clang拒绝它error: array initializer must be an initializer list.但是,GCC 接受它并且似乎用指定临时副本初始化列表中的每个向量.

这是我从未听说过的一些GCC扩展(以某种方式也设法生存-pedantic-errors)或只是一个普通的错误?

c++ gcc

20
推荐指数
1
解决办法
831
查看次数

std :: common_type,引用了type_info

我很困惑:在升级到GCC 6(RC1)之前,一些模板代码使用std::common_type了失败前工作的代码.我试过clang,那也失败了......所以我一定做错了!

代码相当于:

#include <type_traits>
#include <typeinfo>

using namespace std;

// common_type of two const type_info& is ok (compiles ok)
common_type<const type_info&, const type_info&>::type func1();

// common_type of three type_info& is bad...(fails to compile)
common_type<const type_info&, const type_info&, const type_info&>::type func2();

// common_type of two const int& is ok
common_type<const int&, const int&>::type func3();

// common_type of three const int& is ok too!
common_type<const int&, const int&, const int&>::type func4();
Run Code Online (Sandbox Code Playgroud)

common_type具有三个类型参数的第二个std::type_info const &无法编译.clang密码建议我使用两个参数std::common_type,但这是在模板扩展中我无法控制输入! …

c++ templates c++11

20
推荐指数
1
解决办法
291
查看次数

我应该删除移动构造函数和智能指针的移动分配吗?

我正在实现一个简单的智能指针,它基本上跟踪它处理的指针的引用数量.

我知道我可以实现移动语义,但我不认为复制智能指针非常便宜.特别是考虑到它引入了产生令人讨厌的错误的机会.

这是我的C++ 11代码(我省略了一些不必要的代码).欢迎提出一般性意见.

#ifndef SMART_PTR_H_
#define SMART_PTR_H_

#include <cstdint>

template<typename T>
class SmartPtr {
private:
    struct Ptr {
        T* p_;
        uint64_t count_;
        Ptr(T* p) : p_{p}, count_{1} {}
        ~Ptr() { delete p_; }
    };
public:
    SmartPtr(T* p) : ptr_{new Ptr{p}} {}
    ~SmartPtr();

    SmartPtr(const SmartPtr<T>& rhs);
    SmartPtr(SmartPtr<T>&& rhs) =delete;

    SmartPtr<T>& operator=(const SmartPtr<T>& rhs);
    SmartPtr<T>& operator=(SmartPtr<T>&& rhs) =delete;

    T& operator*() { return *ptr_->p_; }
    T* operator->() { return ptr_->p_; }

    uint64_t Count() const { return ptr_->count_; }

    const T* …
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers move-semantics c++11

20
推荐指数
1
解决办法
3006
查看次数

使用使用成员类型别名的构造函数推导类模板参数

所以显然这应该工作:

template<class T>
struct C {
   using value_type = T;
   C(value_type);
};

C c(1); // C<int>
Run Code Online (Sandbox Code Playgroud)

就像这样(参见[over.match.class.deduct]/3中的B例子):

template<class T>
struct D {
   template<class> using meow_t = T;

   template<class U>
   D(U, meow_t<U>);
};

D d(1, 'c'); // D<char>
Run Code Online (Sandbox Code Playgroud)

请注意,看似等效的显式指南不起作用,因为参数是非推导的上下文:

template<class T>
C(typename C<T>::value_type) -> C<T>;
Run Code Online (Sandbox Code Playgroud)

虽然至少第一个片段的作用当然是可取的,但我还没有找到实际使其在当前工作草案中起作用的措辞.有谁知道它在哪里?

c++ language-lawyer c++17

20
推荐指数
1
解决办法
545
查看次数

当NULL不能被nullptr替换时?

我正在重构一些NULL在很多地方使用的旧代码.问题是

盲目地更换所有NULL实例是否安全nullptr?

我特别感兴趣的是,替换NULL by nullptr可能会导致一些运行时错误(编译时错误可以),但我想不出任何错误.如果没有,通过nullptr自动替换NULL是否安全(如果有的话,修复编译时错误).

如果之前已经提出过问题我很抱歉 - 我找不到它,如果你指出我的答案,我会删除它!

c++ null nullptr c++11

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

从函数类型中剥离所有限定符

给定一个可能的varargs函数类型,可能有cv-qualifier-seq和可能的ref-qualifier,是否可以编写一个类型特征来剥离所有限定符而不编写4*3*2 = 24个部分特化?

template<class T>
struct strip_function_qualifiers;

template<class R, class... Args>
struct strip_function_qualifiers<R(Args...)> { using type = R(Args...); };

template<class R, class... Args>
struct strip_function_qualifiers<R(Args..., ...)> { using type = R(Args..., ...); };

template<class R, class... Args>
struct strip_function_qualifiers<R(Args...) const> { using type = R(Args...); };

template<class R, class... Args>
struct strip_function_qualifiers<R(Args..., ...) const > { using type = R(Args..., ...); };

template<class R, class... Args>
struct strip_function_qualifiers<R(Args...) const &> { using type = R(Args...); }; …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11 c++14 c++17

18
推荐指数
1
解决办法
716
查看次数

如何在CentOS 7上通过clang构建libcxx和libcxxabi

我想在CentOS 7上使用带有clang/clang ++的C++ 11或C++ 14.如何构建这个构建环境?

c++ centos clang c++11 libc++

17
推荐指数
1
解决办法
1万
查看次数

在c ++ 11时钟之间转换

如果我有一个time_point任意时钟(比方说high_resolution_clock::time_point),有没有办法将其转换time_point为另一个任意时钟(比方说system_clock::time_point)?

我知道如果存在这种能力,就必须有限制,因为并非所有的时钟都是稳定的,但有没有任何功能可以帮助这些规范中的转换呢?

c++ c++11 c++-chrono

17
推荐指数
2
解决办法
1975
查看次数