我基本上有一个依赖于非类型模板参数的类.我定义了一个转换,所以非类型模板参数的对象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 …
我想为某个对象实现一个范围构造函数,但我想将它限制为只接受两个输入迭代器.
我试图用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) 我在项目中使用了priority_queue,我决定使用仿函数进行初始化,但在互联网上查找我发现:
std::priority_queue<int, std::vector<int>, std::greater<int> > pq;
Run Code Online (Sandbox Code Playgroud)
我已经使用它并且可以工作,但我不确定为什么在std::vector<int>
那里使用以及底层容器是什么以及如何管理它们(我查找信息,但我不明白).