小编mko*_*tya的帖子

.template(点模板)构造用法

可能重复:
我必须在何处以及为何要使用"template"和"typename"关键字?

我遇到了一段奇怪的代码:

#include <iostream>

template <int N>
struct Collection {
  int data[N];

  Collection() {
    for(int i = 0; i < N; ++i) {
      data[i] = 0;
    }
  };

  void SetValue(int v) {
    for(int i = 0; i < N; ++i) {
      data[i] = v;
    }
  };

  template <int I>
  int GetValue(void) const {
    return data[I];
  };
};

template <int N, int I>
void printElement(Collection<N> const & c) {
  std::cout << c.template GetValue<I>() << std::endl; /// doesn't compile without ".template" …
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

用C++中的比较器初始化set

我遇到了以下代码:

#include <iostream>
#include <set>

int main() {

   auto comp = [](int x, int y){return (x > y); };

   std::set<int, decltype(comp)> inversed({1,2,3,4,5}, comp);
   for ( auto i = inversed.begin(); i != inversed.end(); ++i ) {
      std::cout << *i << std::endl;
   }

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

代码打印"5 4 3 2 1",即以反向顺序设置初始值.谁能解释为什么?比较器如何影响集合的初始化?

谢谢,
克斯特亚

c++ set comparator

2
推荐指数
1
解决办法
247
查看次数

如何在VHDL中从内部架构写入两个输出端口?

尝试在VHDL中将组件连接到父层次结构的两个输出端口时遇到问题.由于物理连接只能通过"端口映射"语句完成,因此无法将本地信号连接到多个输出端口.这是一个例子:

在此输入图像描述

上述电路的描述应该是smth.像这样:

entity HIER is
port (
    IN1 : in bit;
    OUT1, OUT2 : out bit);
end hier;

architecture HIER_IMPL of HIER is 
   component BUF is 
      port (a : in bit; o : out bit);
   end component;
begin
   BUF1 : BUF port map (a => IN1, o => OUT1, o => OUT2);
end HIER_IMPL;
Run Code Online (Sandbox Code Playgroud)

但是,输出端口"o"与OUT1和OUT2的双重分配将不起作用,因为它在VHDL中是禁止的.

instantiation vhdl variable-assignment

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

通过构造函数初始化std :: array私有成员

我有以下代码:

#include <iostream>
#include <array>

class Base {
public:
  Base() : mA(std::array<int,2>()) {}
  Base(std::array<int,2> arr) : mA(arr) {}
  Base(/* what to write here ??? */);
private:
  std::array<int,2> mA;
};

int main() 
{
    std::array<int,2> a = {423, 12}; // Works fine
    Base b(a); // Works fine
    Base c({10, 20}); // This is what I need. 

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我应该如何定义构造函数以允许初始化,如上面"main"中的第3行所示?一般来说,我需要一个可配置的(在编译/运行时长度)结构,允许使用数字列表进行初始化,如{1,2,3}或(1,2,3)或类似的东西,而不需要元素 - 按元素复制.为简单起见,我选择了std :: array,但我担心它可能不适用于这种初始化.你会推荐什么容器?

谢谢,克斯特亚

c++ constructor class

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