模板和通用参考

时间:2020-02-10 12:22:23

标签: c++ templates rvalue-reference type-deduction

我正在学习有关创建实体组件框架的教程。它使用组合(一个实体具有一组组件)。

class Component
{
public:
    Entity* entity;

 //VIRTUAL VOID INITIALIZE, DRAW, UPDATE functions here
private:
    std::unique_ptr<Entity> m_pEntity;
};

class Entity
{
public:

//Add component function

    template <typename T, typename... TArgs>
    T& addComponent(TArgs&&... mArgs)
    {
        T* LComponent(new T(std::forward<TArgs>(mArgs)));
        LComponent->entity = this;
        std::unique_ptr<Component> uPtr{ LComponent };
        m_pComponents.emplace_back(std::move(uPtr));
        m_ComponentArray[getComponentTypeID<T>()] = LComponent;
        LComponent->ECS_init();
        return *LComponent;

    }
private:
    bool m_IsActive = true;
    std::vector <std::unique_ptr<Component>> m_pComponents;
    std::array<Component*, gMaxComponents> m_ComponentArray;
};

但是,作者并没有很好地解释其背后的逻辑,而且我无法理解addComponent函数的语法。特别是:

  • 为什么将uPtr投射到右值引用 m_pComponents.emplace_back(std::move(uPtr))时为什么要强制将其out_algo = widgets.Output() out_ht = widgets.Output() outioi_trd = widgets.Output() out_adv = widgets.Output() out_fundexp = widgets.Output() out_stratexp = widgets.Output() tab = widgets.Tab(children = [out_algo,out_ht,outioi_trd,out_adv,out_fundexp,out_stratexp]) tab.set_title(0,'Algo Trades') tab.set_title(1,'HT Trades') tab.set_title(2,'IOI Trades') tab.set_title(3,'Top 20 ADV Trades') tab.set_title(4,'Fund Exposure') tab.set_title(5,'Strategy Exposure') display(tab) broker_list_test = broker_list_test.set_index('Broker') #drop columns not needed broker_list_test = broker_list_test.drop(['Direction', 'Investment_Team', 'portfolio_name', 'full_name','Desk', 'trader', 'trade_date', 'pct_adv_1m', 'trade_qty', 'price'],axis=1) #group by broker and USD notional sum broker_list_test2 = broker_list_test.groupby('Broker') broker_list_test3 = broker_list_test2.sum() #group by value per broker broker_list_test3['USD_Notional']=broker_list_test3['USD_Notional'].astype('float') #IF WE HAVE COMMA USE s.replace(',','.') label = broker_list_test3.index.tolist() #pull out all index names as pie labels data = broker_list_test3.values.tolist() # pie data data = reduce(lambda x,y: x+y,data) #break list of lists into one list data_float=[float(x) for x in data] #convert string to float sum_algo=0 for x in data_float: sum_algo+=x with out_algo: plt.pie(data_float,labels=label,autopct="%0.f%%",radius=2.4) plt.title("Algo Usage Notional Breakdown in USD\n"+'{:,.0f}'.format(sum_algo),position=(0.5,1.5)) plt.show() 转换为右值引用overflow: TextOverflow.ellipsis

1 个答案:

答案 0 :(得分:1)

为什么需要将int main(int argc,char *argv[]) { int ret; int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; memset(&server_address,0,sizeof(server_address)); memset(&client_address,0,sizeof(client_address)); int result; fd_set readfds, testfds; int maxfd; int on=1; server_sockfd = rsocket(AF_INET, SOCK_STREAM, 0); setsockopt(server_sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)); server_address.sin_family = AF_INET; server_address.sin_port = htons(8888); server_address.sin_addr.s_addr = htonl(INADDR_ANY); ret = rbind(server_sockfd,(struct sockaddr *)&server_address,sizeof(server_address)); printf("rbind retunr %d\n", ret); ret = rlisten(server_sockfd,5); printf("rlisten retunr %d\n", ret); //set fd_set FD_ZERO(&readfds); FD_SET(server_sockfd,&readfds); maxfd = server_sockfd + 1; while(1) { char ch; int fd,i; int nread; testfds = readfds; result = rselect(FD_SETSIZE, &testfds, NULL, NULL, NULL); printf("rselect retunr %d\n", result); if(result < 1) { printf("server5\n"); exit(1); } for( fd = 1; fd < maxfd; fd++) { if(FD_ISSET(fd,&testfds)) { if(fd == server_sockfd) { memset(&client_address,0,sizeof(client_address)); client_len = sizeof(client_address); client_sockfd = raccept(server_sockfd,(struct sockaddr *)&client_address,&client_len); if(fork()==0) { setsockopt(client_sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)); rclose(server_sockfd); ret = rrecv(client_sockfd,&ch,1,0); printf("recv from client : %c, ret %d\n",ch, ret); sleep(2); ch++; ret = rsend(client_sockfd,&ch,1,0); printf("rsend to client : %c, ret %d, erro %d\n",ch, ret, errno); } } } } } } 转换为右值引用 uPtr将其添加到 向量

这是因为m_pComponents.emplace_back(std::move(uPtr))的类型为uPtr。此类型无法复制!它只能被移动。这是为了确保只有一个唯一的句柄拥有指针的所有权。