小编Fra*_*ido的帖子

Clang和GCC在铸造C++ 17中对非类型模板参数的auto说明符不同意

我基本上有一个依赖于非类型模板参数的类.我定义了一个转换,所以非类型模板参数的对象N可以转换为另一个M.我有一个可以重现这种情况的最小例子:

template<auto Integral>
class Test{
    public:
        typedef decltype(Integral) value_type;
        static constexpr value_type N = Integral;

        constexpr Test (const value_type& x = 0);

        template<auto Integral2>
        constexpr explicit operator Test<Integral2>() const;
    private:
        value_type n;
};

template<auto Integral>
constexpr Test<Integral>::Test (const value_type& x){
    if (x < 0){
        n = N - (-x)%N;
    }
    else{
        n = x%N;
    }
}

template<auto Integral> template<auto Integral2>
constexpr Test<Integral>::operator Test<Integral2>() const{
    return Test<Integral2>(n%(Test<Integral2>::N));
}
Run Code Online (Sandbox Code Playgroud)

我正在使用GCC 7.2.0和Clang 5.0.0在Ubuntu 16.04 LTS中使用标志进行编译-O2 -std=c++17.

问题是我一直在使用g …

c++ g++ language-lawyer clang++ c++17

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

如何实现构造函数,以便它只接受使用typeid的输入迭代器?

我想为某个对象实现一个范围构造函数,但我想将它限制为只接受两个输入迭代器.

我试图用gcc 7.1.0编译这段代码.

文件 test.cpp

#include <vector>
#include <type_traits>
#include <typeinfo>

template <typename Iterator>
using traits = typename std::iterator_traits<Iterator>::iterator_category;

template <typename T>
class A{
   private:

      std::vector<T> v;

   public:

      template <typename InputIterator,
               typename = std::enable_if_t<
                  typeid(traits<InputIterator>) ==
                  typeid(std::input_iterator_tag)>
               >
      A(InputIterator first, InputIterator last) : v(first, last) {}
};

int main(){
   std::vector<double> v = {1, 2, 3, 4, 5};
   A<double> a(v.begin(), v.end());
}
Run Code Online (Sandbox Code Playgroud)

我得到这个编译错误g++ test.cpp -o test:

  test.cpp: In function ‘int main()’:
  test.cpp:27:34: error: no matching function for call to …
Run Code Online (Sandbox Code Playgroud)

c++ constructor iterator sfinae c++14

5
推荐指数
1
解决办法
434
查看次数

什么是底层容器?

我在项目中使用了priority_queue,我决定使用仿函数进行初始化,但在互联网上查找我发现:

std::priority_queue<int, std::vector<int>, std::greater<int> > pq;
Run Code Online (Sandbox Code Playgroud)

我已经使用它并且可以工作,但我不确定为什么在std::vector<int>那里使用以及底层容器是什么以及如何管理它们(我查找信息,但我不明白).

c++

0
推荐指数
1
解决办法
319
查看次数

标签 统计

c++ ×3

c++14 ×1

c++17 ×1

clang++ ×1

constructor ×1

g++ ×1

iterator ×1

language-lawyer ×1

sfinae ×1