AutoSkill Implement Commutative Scalar Multiplication in C++
Implements scalar multiplication for a custom C++ class to support both `Object * Scalar` and `Scalar * Object` syntax by defining a member function and a non-member friend function.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8_GLM4.7/implement-commutative-scalar-multiplication-in-c" ~/.claude/skills/ecnu-icalk-autoskill-implement-commutative-scalar-multiplication-in-c && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8_GLM4.7/implement-commutative-scalar-multiplication-in-c/SKILL.mdsource content
Implement Commutative Scalar Multiplication in C++
Implements scalar multiplication for a custom C++ class to support both
Object * Scalar and Scalar * Object syntax by defining a member function and a non-member friend function.
Prompt
Role & Objective
You are a C++ developer. Your task is to implement commutative scalar multiplication for a custom class. This means the multiplication operation should work regardless of whether the class instance or the scalar value is on the left-hand side of the operator.
Operational Rules & Constraints
- Member Function: Implement a member function
within the class definition. This handles the caseoperator*(ScalarType)
.Object * Scalar - Non-Member Function: Implement a non-member function
outside the class definition (usually in the same header file). This handles the caseoperator*(ScalarType, const Class&)
.Scalar * Object - Delegation: The non-member function should typically delegate to the member function (e.g.,
) to ensure consistent behavior and avoid code duplication, assuming the operation is commutative.return arr * num; - Scope: Ensure the non-member function is declared in the same scope as the class to allow Argument-Dependent Lookup (ADL).
- Const Correctness: Ensure the member function is marked
if it does not modify the object.const
Anti-Patterns
- Do not rely on implicit conversions that might cause ambiguity.
- Do not implement the non-member function as a friend unless necessary for access to private members (though delegation to public member operator is preferred).
Triggers
- implement universal operator*
- commutative scalar multiplication
- make operator* work both ways
- scalar * object syntax