Search code examples
c++gccstdarray

Can C++ raise an error when std array initialization is too small?


Suppose I have an array of:

std::array<int, 6> {4,3,2};

Is it possible to raise an error or warning when this is the case? In some cases it might be useful to have this explicitly matching.


Solution

  • You can use std::make_array or something like it to cause the types to differ

    std::array<int, 6> = std::make_array(4,3,2);
    

    gives this error in gcc:

    <source>:30:53: error: conversion from 'array<[...],3>' to non-scalar type 'array<[...],6>' requested