diff --git a/src/Generator/Passes/ConstructorToConversionOperatorPass.cs b/src/Generator/Passes/ConstructorToConversionOperatorPass.cs index 7d8bfefa..1e7f34f0 100644 --- a/src/Generator/Passes/ConstructorToConversionOperatorPass.cs +++ b/src/Generator/Passes/ConstructorToConversionOperatorPass.cs @@ -1,4 +1,5 @@ -using CppSharp.AST; +using System.Linq; +using CppSharp.AST; using CppSharp.AST.Extensions; namespace CppSharp.Passes @@ -12,9 +13,25 @@ namespace CppSharp.Passes public override bool VisitMethodDecl(Method method) { if (AlreadyVisited(method) || !method.IsGenerated || !method.IsConstructor || - method.IsCopyConstructor || method.Parameters.Count != 1) + method.IsCopyConstructor) return false; + var @params = method.Parameters.Where(p => p.Kind == ParameterKind.Regular).ToList(); + if (@params.Count == 0) + return false; + + if (Driver.Options.GenerateDefaultValuesForArguments) + { + var nonDefaultParams = @params.Count(p => p.DefaultArgument == null); + if (nonDefaultParams > 1) + return false; + } + else + { + if (@params.Count != 1) + return false; + } + var parameter = method.Parameters[0]; // TODO: disable implicit operators for C++/CLI because they seem not to be support parameters if (!Driver.Options.IsCSharpGenerator) diff --git a/tests/CSharpTemp/CSharpTemp.Tests.cs b/tests/CSharpTemp/CSharpTemp.Tests.cs index 123e9d14..978b0fcd 100644 --- a/tests/CSharpTemp/CSharpTemp.Tests.cs +++ b/tests/CSharpTemp/CSharpTemp.Tests.cs @@ -433,4 +433,13 @@ public class CSharpTempTests : GeneratorTestFixture ITestParamToInterfacePassBaseTwo interfaceTypePtr; obj.FuncTryInterfaceTypePtrOut(out interfaceTypePtr); } + + [Test] + public void TestConversionForCtorWithDefaultParams() + { + using (Foo foo = 15) + { + Assert.That(foo.A, Is.EqualTo(15)); + } + } } diff --git a/tests/CSharpTemp/CSharpTemp.cpp b/tests/CSharpTemp/CSharpTemp.cpp index cee8e016..6a4c85f5 100644 --- a/tests/CSharpTemp/CSharpTemp.cpp +++ b/tests/CSharpTemp/CSharpTemp.cpp @@ -6,6 +6,12 @@ Foo::Foo() P = 50; } +Foo::Foo(int a, int p) +{ + A = a; + P = p; +} + int Foo::method() { return 1; diff --git a/tests/CSharpTemp/CSharpTemp.h b/tests/CSharpTemp/CSharpTemp.h index d6ec3ce8..a7def1d1 100644 --- a/tests/CSharpTemp/CSharpTemp.h +++ b/tests/CSharpTemp/CSharpTemp.h @@ -5,6 +5,7 @@ class DLL_API Foo { public: Foo(); + Foo(int a, int p = 0); int method(); int operator[](int i) const; int operator[](unsigned int i); @@ -706,4 +707,4 @@ class DLL_API TestOutTypeInterfaces public: void funcTryInterfaceTypePtrOut(CS_OUT TestParamToInterfacePassBaseTwo* classTry); void funcTryInterfaceTypeOut(CS_OUT TestParamToInterfacePassBaseTwo classTry); -}; \ No newline at end of file +};