Skip to content

IEC_ARRAY_1D brace-initializer fails template deduction when integer literals span more than one C++ rank #207

Description

@dreamclass

Problem

Summary

When an array variable is initialized from a brace list of integer literals
whose values span more than one C++ integer rank (some fit in int, some
require long/long long), the generated IEC_ARRAY_1D constructor call fails
template argument deduction. strucpp codegen succeeds, but the resulting C++
does not compile.

Environment

  • STruC++ version: 0.6.2, runtime iec_array.hpp
  • C++ compiler: g++ (MSYS2 UCRT64) 15.1.0, -std=c++17

Impact

Compilation fails.

Reproduction

arr1d.st:

FUNCTION_BLOCK FB
VAR
    seeds : ARRAY [0..3] OF UDINT := [5489, 1301868182, 2938499221, 2950281878];
END_VAR
END_FUNCTION_BLOCK

PROGRAM PLC_PRG
VAR
    inst : FB;
    r : UDINT;
END_VAR
    inst();
    r := inst.seeds[0];
END_PROGRAM
strucpp arr1d.st -o arr1d.cpp --no-default-libs
g++ -std=c++17 -I<strucpp>/runtime/include -c arr1d.cpp

(2938499221 and 2950281878 exceed INT_MAX; 5489 does not.)

Actual result

strucpp reports Compilation successful! and emits, in the FB constructor:

: SEEDS({5489, 1301868182, 2938499221, 2950281878})

g++ then fails:

arr1d.cpp:57:7: error: no matching function for call to
  'strucpp::IEC_ARRAY_1D<strucpp::IECVar<unsigned int>, strucpp::ArrayBounds<0, 3> >
   ::IEC_ARRAY_1D(<brace-enclosed initializer list>)'
   57 |     : SEEDS({5489, 1301868182, 2938499221, 2950281878})
iec_array.hpp:63: note: candidate: 'template<class U> IEC_ARRAY_1D(std::initializer_list<U>)'
iec_array.hpp:63: note:   template argument deduction/substitution failed:
arr1d.cpp:57: note:   deduced conflicting types for parameter '_Tp' ('int' and 'long long int')

std::initializer_list<U> must deduce a single U; because 5489 is typed
int and the larger values are typed long long, deduction fails.

Code Locations

No response

Fix

No response

Acceptance

The array constant compiles regardless of the individual literals' natural C++
ranks — every element belongs to the array's declared element type (UDINT).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions