What Is a "Span" and When Should I Use One

Why in particular should I rather pass a std::span than a std::vector& to a function?

The equivalent of passing a std::vector<int> const& is not std::span<int> const, but rather std::span<int const>. The span itself being const or not won't really change anything, but more const is certainly good practice.

So when should you use it?

I would say that it entirely depends on the body of the function, which you omitted from your examples.

For example, I would still pass a vector around for this kind of functions:

std::vector<int> stored_vec;

void store(std::vector<int> vec) {
stored_vec = std::move(vec);
}

This function does store the vector, so it needs a vector. Here's another example:

void needs_vector(std::vector<int> const&);

void foo(std::vector<int> const& vec) {
needs_vector(vec);
}

As you can see, we need a vector. With a span you would have to create a new vector and therefore allocate.


For this kind of functions, I would pass a span:

auto array_sum(std::span<int const> const values) -> int {
auto total = int{0};

for (auto const v : values) {
total += v;
}

return total;
}

As you can see, this function don't need a vector.

Even if you need to mutate the values in the range, you can still use span:

void increment(std::span<int> const values) {
for (auto& v : values) {
++v;
}
}

For things like getter, I will tend to use a span too, in order to not expose direct references to members from the class:

struct Bar {
auto get_vec() const -> std::span<int const> {
return vec;
}

private:
std::vector<int> vec;
};

What is the difference between p, div and span in HTML&XHTML?

p and div elements are block level elements where span is an inline element and hence margin on span wont work. Alternatively you can make your span a block level element by using CSS display: block; or for span I would prefer display: inline-block;

Apart from that, these elements have specific semantic meaning, div is better referred for a block of content having different nested elements, p which is used for paragraphs, and span is nothing but an empty element, hence keeping SEO in mind, you need to use right tag for right thing, so for example wrapping the text inside div element will be less semantic than wrapping it inside a p

What is the difference between HTML div and span elements?

  • div is a block element
  • span is an inline element.

This means that to use them semantically, divs should be used to wrap sections of a document, while spans should be used to wrap small portions of text, images, etc.

For example:

<div>This a large main division, with <span>a small bit</span> of spanned text!</div>

Note that it is illegal to place a block-level element within an inline element, so:

<div>Some <span>text that <div>I want</div> to mark</span> up</div>

...is illegal.


EDIT: As of HTML5, some block elements can be placed inside of some inline elements. See the MDN reference here for a pretty clear listing. The above is still illegal, as <span> only accepts phrasing content, and <div> is flow content.


You asked for some concrete examples, so is one taken from my bowling website, BowlSK:

<div id="header">  <div id="userbar">    Hi there, <span class="username">Chris Marasti-Georg</span> |    <a href="/edit-profile.html">Profile</a> |    <a href="https://www.bowlsk.com/_ah/logout?...">Sign out</a>  </div>  <h1><a href="/">Bowl<span class="sk">SK</span></a></h1></div>

Should I use p or span element for a single line of text?

The HTML5 spec defines that the p element "represents a paragraph", and a paragraph is defined as:

A paragraph is typically a run of phrasing content that forms a block of text with one or more sentences that discuss a particular topic, as in typography, but can also be used for more general thematic grouping. For instance, an address is also a paragraph, as is a part of a form, a byline, or a stanza in a poem.

No-one can generally answer if you should use p or not, this depends on each particular case, and also your understanding of the content.

Now, if you think p may not be appropriate, why do you want to go with span when you are looking for a block element? Just use div instead.

How do you choose when to use DIV and when SPAN, to wrap something?

As you note, you should use divs as dividers of blocks, and spans for marking inline content.

And yes, you should try to avoid changing the display types of them.

Why Use Span for Radio Button or Checkbox Labels?

The <span> HTML element is a generic inline container for phrasing content, which does not inherently represent anything. It can be used to group elements for styling purposes (using the class or id attributes), or because they share attribute values, such as lang. It should be used only when no other semantic element is appropriate. <span> is very much like a <div> element, but <div> is a block-level element whereas a <span> is an inline element.

see this:- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/span

Label vs span: HTML

A label is used when you have a form or input elements - the label is associated with an input element. Span is a general container for any inline content. I think you want a span in this case



Related Topics



Leave a reply



Submit