小编Alb*_*ini的帖子

A <T>的朋友也可以成为A <A <T >>的朋友吗?

请考虑以下代码:

#include <vector>

template<typename T> class Container;
template<typename T> Container<Container<T>> make_double_container(const std::vector<std::vector<T>>&);

template<typename T>
class Container {
    std::vector<T> v;
    friend Container<Container<T>> make_double_container<T>(const std::vector<std::vector<T>>&);

public:
    Container() {}
    explicit Container(std::vector<T> v) : v(v) {}
};

template<typename T>
Container<Container<T>> make_double_container(const std::vector<std::vector<T>>& v) {
    Container<Container<T>> c;
    for(const auto& x : v) {
        c.v.push_back(Container<T>(x));
    }
    return c;
}

int main() {
    std::vector<std::vector<int>> v{{1,2,3},{4,5,6}};
    auto c = make_double_container(v);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器告诉我:

main.cpp: In instantiation of 'Container<Container<T> > make_double_container(const std::vector<std::vector<T> >&) [with T = int]':
main.cpp:27:37: …
Run Code Online (Sandbox Code Playgroud)

c++ friend friend-function c++14

15
推荐指数
1
解决办法
517
查看次数

has_many:through,:source,:source_type返回空数组

我得到了一些ManagerSoccerTeam模特.一名经理"拥有"许多足球队; 经理也可以评论足球队,也可以评论其他经理:

manager.rb

# Soccer teams the manager owns
has_many :soccer_teams, :dependent => :restrict
# Comments the manager has made on soccer teams or other managers
has_many :reviews, :class_name => "Comment", :foreign_key => :author_id, :dependent => :destroy
# Comments the manager has received by other managers
has_many :comments, :as => :commentable, :dependent => :destroy
# Soccer teams that have received a comment by the manager
has_many :observed_teams, :through => :comments, :source => :commentable, :source_type => "SoccerTeam" …
Run Code Online (Sandbox Code Playgroud)

model-view-controller ruby-on-rails has-many has-many-polymorphs has-many-through

7
推荐指数
1
解决办法
6133
查看次数

在Rails应用程序中改进不引人注目的javascript(并可能使用CoffeeScript)

我有一个应用程序,它使用一些Javascript来执行基本的Ajax请求,例如自动完成和实时搜索.例如,我通过以下方式实现了实时搜索; 我发现了一些潜在的问题,并希望与您讨论这个问题,以便有更好的代码.

应用程序/控制器/ company_controller.rb

def livesearch
  @companies = Company.search(params[:query])
  render :partial => "companies", :locals => {:companies => @companies}
end
Run Code Online (Sandbox Code Playgroud)

应用程序/视图/公司/ _companies.html.haml

- if companies.empty?
  None
- else
  %table#company_list
    %tr
      %th Name
      %th Description
      %th Products
      %th
    = render companies
Run Code Online (Sandbox Code Playgroud)

应用程序/视图/公司/ _livesearch_box.html.haml

= content_for :scripts, "jlivesearch companies"
= form_tag "#", :autocomplete => :off, :remote => true do
  %span.light
    Search: &nbsp;
  = text_field_tag :search
  :javascript
    $('#search').livesearch({
      searchCallback: update_listed_companies,
      queryDelay: 200,
      innerText: "Search companies"
    });
Run Code Online (Sandbox Code Playgroud)

公共/ Java脚本/ companies.js

function update_listed_companies(query) {
  if(typeof query …
Run Code Online (Sandbox Code Playgroud)

javascript ruby-on-rails dry unobtrusive-javascript coffeescript

6
推荐指数
1
解决办法
1499
查看次数

将Rails 3转换为Rails 2:使用块的帮助程序

在Rails 3中,我使用以下帮助程序来获得奇怪的颜色表:

def bicolor_table(collection, classes = [], &block)
  string = ""
  even = 0
  for item in collection
    string << content_tag(:tr, :class => (((even % 2 == 0) ? "even " : "odd ") + classes.join(" "))) do
        yield(item)
    end
    even = 1 - even
  end
  return string
end
Run Code Online (Sandbox Code Playgroud)

我在我的观点中使用它:

<%= bicolor_table(services) do |service| %>
    <td><%= image_tag service.area.small_image %></td>
    <td><%= link_to service.title, service %></td>
<% end %>
Run Code Online (Sandbox Code Playgroud)

现在,我必须将应用程序迁移到Rails 2.问题是Rails 2不使用Erubis,因此当它找到<%= whatever%>标记时,它只调用whatever.to_s.所以,在我的情况下,这导致尝试执行

(bicolor_table(services) do |service|).to_s
Run Code Online (Sandbox Code Playgroud)

带来明显(坏)的后果.问题是:我原则上是错的(比如"帮助者不应该这样工作,而是使用......")或者我应该寻找解决方法吗?

谢谢.

ruby-on-rails helper view-helpers

4
推荐指数
1
解决办法
754
查看次数

使用 std::views::chunk 编译此代码时,为什么会出现错误:无法分解无法访问的成员?

此代码可以正确编译并运行:

\n
#include <ranges>\n#include <iostream>\n\nint main() {\n    const auto r = std::views::iota(\'a\', static_cast<char>(\'g\' + 1));\n\n    for(const auto& [start, end] : r | std::views::chunk(3u)) {\n        for(auto it = start; it != end; ++it) {\n            std::cout << *it << " ";\n        }\n        std::cout << "\\n";\n    }\n\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

其输出为:

\n
a b c \nd e f \ng \n
Run Code Online (Sandbox Code Playgroud)\n

如果我将定义更改r如下:

\n
a b c \nd e f \ng \n
Run Code Online (Sandbox Code Playgroud)\n

该代码无法编译。GCC 13 发出以下错误:

\n
chunk.cpp:13:21: error: cannot decompose inaccessible …
Run Code Online (Sandbox Code Playgroud)

c++ c++20 std-ranges c++23

3
推荐指数
1
解决办法
362
查看次数

将2个指针传递给2个线程,但它们最终共享相同的内容

我想这个问题已经出现了,它肯定会在线程世界中显示我的初学者级别,但我无法找到任何先前的问题或其他资源来解决它.我已经完成了对C++ 11线程最常见的介绍(比如这个,这个这个),但它没有帮助.

这是我的代码:

mutex mtx;
vector<thread> threads;

for(vcit = vc.begin(); vcit != vc.end(); ++vcit) {
    const std::shared_ptr<Graph> g = graphs.at(*vcit);

    cout << "Graph (outside thread): " << g->name << endl;
    threads.push_back(thread(
        [&g, &mtx] () {
            lock_guard<mutex> guard(mtx);
            cout << "Graph (inside thread): " << g->name << endl;
        }
    ));
}

for(thread& t : threads) {
    t.join();
} 
Run Code Online (Sandbox Code Playgroud)

我希望每个线程都接收不同的指针,但是程序的输出如下(对于向量中的2个元素vc):

Graph (outside thread): ABC
Graph (outside thread): DEF
Graph (inside thread): DEF
Graph (inside …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading thread-safety shared-ptr c++11

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