ęœ€ēµ‚ę›“ę–°ę—„ę™‚:
ćŒę›“ę–°

屄歓 編集

function template
<memory>

std::uninitialized_fill

namespace std {
  template <class ForwardIterator, class T>
  void
    uninitialized_fill(ForwardIterator first,
                       ForwardIterator last,
                       const T& x);           // (1) C++98
  template <class NoThrowForwardIterator,
            class T = typename iterator_traits<NoThrowForwardIterator>::value_type>
  constexpr void
    uninitialized_fill(NoThrowForwardIterator first,
                       NoThrowForwardIterator last,
                       const T& x);           // (1) C++26

  template <class ExecutionPolicy, class ForwardIterator, class T>
  void
    uninitialized_fill(ExecutionPolicy&& exec,
                       ForwardIterator first,
                       ForwardIterator last,
                       const T& x);           // (2) C++17
  template <class ExecutionPolicy,
            class NoThrowForwardIterator,
            class T = typename iterator_traits<NoThrowForwardIterator>::value_type>
  void
    uninitialized_fill(ExecutionPolicy&& exec,
                       NoThrowForwardIterator first,
                       NoThrowForwardIterator last,
                       const T& x);           // (2) C++26
}

ꦂ要

ęœŖåˆęœŸåŒ–é ˜åŸŸć®ēÆ„å›²ć‚’ć€ęŒ‡å®šć•ć‚ŒćŸå€¤ć§é…ē½®new恙悋怂

ęœŖåˆęœŸåŒ–å…„åŠ›ć‚¤ćƒ†ćƒ¬ćƒ¼ć‚æēÆ„å›²[first, last)ć‚’åˆęœŸåŒ–ć—ć¤ć¤å€¤xć§åŸ‹ć‚ć‚‹ć€‚

åŠ¹ęžœ

  • C++98 : 仄下と等侔

    for (; first != last; ++first)
      ::new (static_cast<void*>(&*first))
         typename iterator_traits<ForwardIterator>::value_type(x);
    

  • C++17 : 仄下と等侔

    for (; first != last; ++first)
      ::new (static_cast<void*>(addressof(*first)))
         typename iterator_traits<ForwardIterator>::value_type(x);
    

ęˆ»ć‚Šå€¤

なし

例外

å‘¼ć³å‡ŗć™ć‚³ćƒ³ć‚¹ćƒˆćƒ©ć‚Æć‚æćŖć©ć‹ć‚‰ä¾‹å¤–ćŒé€å‡ŗć•ć‚ŒćŸå “åˆć€ćć®ä¾‹å¤–ćŒć“ć®é–¢ę•°ć®å¤–å“ć«ä¼ę’­ć•ć‚Œć‚‹å‰ć«ć€ćć®ę™‚ē‚¹ć§ę§‹ēÆ‰ęøˆć®ć‚Ŗćƒ–ć‚øć‚§ć‚ÆćƒˆćÆå…Øć¦ęœŖč¦å®šć®é †åŗć§ē “ę£„ć•ć‚Œć‚‹ć€‚ć™ćŖć‚ć”ć€ä¾‹å¤–ćŒé€å‡ŗć•ć‚ŒćŸå “åˆćÆåˆęœŸåŒ–åÆ¾č±”é ˜åŸŸćÆęœŖåˆęœŸåŒ–ć®ć¾ć¾ćØćŖć‚‹ć€‚

å‚™č€ƒ

  • (1), (2) :
    • C++26 : å¼•ę•°ćØć—ć¦ę³¢ć‚«ćƒƒć‚³åˆęœŸåŒ–{}ć‚’å—ć‘ä»˜ć‘ć‚‹
      std::uninitialized_fill(p, p + size, {a, b});
      

例

åŸŗęœ¬ēš„ćŖä½æć„ę–¹

#include <iostream>
#include <memory>
#include <algorithm>

int main()
{
  std::allocator<int> alloc;

  // ćƒ”ćƒ¢ćƒŖē¢ŗäæć€‚
  // ć“ć®ę®µéšŽć§ćÆć€[p, p + size)ć®é ˜åŸŸćÆęœŖåˆęœŸåŒ–
  const std::size_t size = 3;
  int* p = alloc.allocate(size);

  // ęœŖåˆęœŸåŒ–é ˜åŸŸpć‚’åˆęœŸåŒ–ć—ć¤ć¤ć€å€¤2ć§åŸ‹ć‚ć‚‹
  std::uninitialized_fill(p, p + size, 2);

  // pć®é ˜åŸŸćŒåˆęœŸåŒ–ć•ć‚Œć€ć‹ć¤ēÆ„å›²pć®å…Øć¦ć®č¦ē“ ćŒ2ć§åŸ‹ć‚ć‚‰ć‚Œć¦ć„ć‚‹ć‹ē¢ŗčŖ
  std::for_each(p, p + size, [](int x) {
    std::cout << x << std::endl;
  });

  // 要瓠を砓棄
  for (std::size_t i = 0; i < size; ++i) {
    std::destroy_at(p + i);
  }

  // ćƒ”ćƒ¢ćƒŖč§£ę”¾
  alloc.deallocate(p, size);
}

å‡ŗåŠ›

2
2
2

ę³¢ć‚«ćƒƒć‚³åˆęœŸåŒ–ć‚’å…„åŠ›ćØć—ć¦ä½æē”Øć™ć‚‹ (C++26)

#include <iostream>
#include <memory>

struct Point {
  int x;
  int y;
};

int main()
{
  std::allocator<Point> alloc;

  const std::size_t size = 3;
  Point* p = alloc.allocate(size);

  // ę³¢ć‚«ćƒƒć‚³åˆęœŸåŒ–ć‚’ē›“ęŽ„ęø”ć›ć‚‹
  std::uninitialized_fill(p, p + size, {1, 2});

  for (std::size_t i = 0; i < size; ++i) {
    std::cout << p[i].x << "," << p[i].y << std::endl;
  }

  for (std::size_t i = 0; i < size; ++i) {
    std::destroy_at(p + i);
  }
  alloc.deallocate(p, size);
}

å‡ŗåŠ›

1,2
1,2
1,2

関連項目

å‚ē…§