Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add struct to vector: "no matching constructor for initialization" error when compiling on macos

I’m using emplace_back to add structs to a std::vector as follows:

struct ConfigurableShaderParameter
{
    std::string variableName;
    std::string description;
    float defaultValue;
    float min;
    float max;
    float step;
    float value;
};

std::vector<ConfigurableShaderParameter> parameters;

std::string variableName, description;
float defaultValue, min, max, step;
// Initialize the above variables
parameters.emplace_back(variableName, description, defaultValue, min, max, step, defaultValue);

The project is built using CMake and C++20 and it’s cross-platform: it compiles fine under Ubuntu and Windows but I cannot get it to compile under macos-latest (I’m using github actions, I don’t have access to a mac).

This is the error message:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

/Users/runner/work/epoch/epoch/src/frontend/src/ConfigurableShader.cpp:41:30: note: in instantiation of function template specialization 'std::vector<epoch::frontend::ConfigurableShaderParameter>::emplace_back<std::string &, std::string &, float &, float &, float &, float &, float &>' requested here
                m_parameters.emplace_back(variableName, description, defaultValue, min, max, step, defaultValue);
                             ^
/Applications/Xcode_14.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/c++/v1/__memory/construct_at.h:35:16: note: candidate template ignored: substitution failure [with _Tp = epoch::frontend::ConfigurableShaderParameter, _Args = <std::string &, std::string &, float &, float &, float &, float &, float &>]: no matching constructor for initialization of 'epoch::frontend::ConfigurableShaderParameter'
constexpr _Tp* construct_at(_Tp* __location, _Args&& ...__args) {
               ^
1 error generated.

My main CMakeLists.txt file includes the following directives and nothing else:

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

The project is open-source, so if you wish you can see the entire failed build message here: https://github.com/ghidosoft/epoch/actions/runs/7992320133/job/21825555388?pr=1#step:5:226

What can I do to fix the error?

Thank you.

>Solution :

At issue here is that until C++20, when initializing an aggregate you have to use {} for the initializer like

foo bar{baz};

In C++20 that rule was updated to allow () to work with aggregates. This was introduced via P0960R3 but it has not yet been included in apple-clang which is why you are getting the error only with that compiler.

To work around this you can give ConfigurableShaderParameter a proper constructor like

struct ConfigurableShaderParameter
{
    ConfigurableShaderParameter(std::string variableName, std::string description, float defaultValue, float min, float max, float step, float value)
       : variableName(variableName), description(description), defaultValue(defaultValue), min(min), max(max), value(value) {}
    std::string variableName;
    std::string description;
    float defaultValue;
    float min;
    float max;
    float step;
    float value;
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading