How to conditionally define a lambda? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Are functors actually faster than pointers to functions?How do you set, clear, and toggle a single bit?How do I iterate over the words of a string?How to convert a std::string to const char* or char*?How can I profile C++ code running on Linux?Why are #ifndef and #define used in C++ header files?Undefined, unspecified and implementation-defined behaviorC++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?What is a lambda expression in C++11?What is an undefined reference/unresolved external symbol error and how do I fix it?Average values of a MAT channel

Recursive calls to a function - why is the address of the parameter passed to it lowering with each call?

Does using the Inspiration rules for character defects encourage My Guy Syndrome?

Married in secret, can marital status in passport be changed at a later date?

Unix AIX passing variable and arguments to expect and spawn

Why isn't everyone flabbergasted about Bran's "gift"?

How to ask rejected full-time candidates to apply to teach individual courses?

Is "ein Herz wie das meine" an antiquated or colloquial use of the possesive pronoun?

Is the Mordenkainen's Sword spell underpowered?

What's the difference between using dependency injection with a container and using a service locator?

Kepler's 3rd law: ratios don't fit data

How can I introduce the names of fantasy creatures to the reader?

lm and glm function in R

What documents does someone with a long-term visa need to travel to another Schengen country?

Why does BitLocker not use RSA?

Suing a Police Officer Instead of the Police Department

How to produce a PS1 prompt in bash or ksh93 similar to tcsh

Why does my GNOME settings mention "Moto C Plus"?

Assertions In A Mock Callout Test

Who can become a wight?

Does traveling In The United States require a passport or can I use my green card if not a US citizen?

How to get a single big right brace?

What is the ongoing value of the Kanban board to the developers as opposed to management

Why do C and C++ allow the expression (int) + 4*5?

How to mute a string and play another at the same time



How to conditionally define a lambda?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Are functors actually faster than pointers to functions?How do you set, clear, and toggle a single bit?How do I iterate over the words of a string?How to convert a std::string to const char* or char*?How can I profile C++ code running on Linux?Why are #ifndef and #define used in C++ header files?Undefined, unspecified and implementation-defined behaviorC++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?What is a lambda expression in C++11?What is an undefined reference/unresolved external symbol error and how do I fix it?Average values of a MAT channel



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








10















The following function will randomly "sprinkle salt" on a loaded image. For the sake of boosting performance, the conditional statement



uint j = rows == 1 ? 0 : randomRow(generator);


should not be inside the loop.



Instead, I want to define a lambda getJ before the loop as



auto getJ = rows == 1 ? []() return 0; : []() return randomRow(generator); ;


However, my code with this lambda does not compile with the following red squiggled text:



enter image description here



Question



How to conditionally define such a lambda?



void salt_(Mat mat, unsigned long long n)

const uchar channels = mat.channels();
uint cols = mat.cols;
uint rows = mat.rows;

if (mat.isContinuous())

cols *= rows;
rows = 1;


default_random_engine generator;
uniform_int_distribution<uint> randomRow(0, rows - 1);
uniform_int_distribution<uint> randomCol(0, cols - 1);



// auto getJ = rows == 1 ? []() return 0; : []() return randomRow(generator); ;


uchar * const data = mat.data;

for (unsigned long long counter = 0; counter < n; counter++)

uint i = randomCol(generator);
uint j = rows == 1 ? 0 : randomRow(generator);
//uint j = getJ();

uint index = channels * (cols * j + i);
for (uchar k = 0; k < channels; k++)
data[index + k] = 255;











share|improve this question



















  • 3





    "my code with this lambda does not compile" - What is the error you get?

    – Suma
    Apr 16 at 12:35











  • You are trying to reference local variable generator without capturing it. If second lambda was capture-free it would compile.

    – VTT
    Apr 16 at 12:36







  • 5





    Seems like a premature optimisation. Surely function call overhead would be greater than ternary overhead?

    – Artyer
    Apr 16 at 12:40






  • 2





    Is a conditional really a performance issue here? Have you profiled the code?

    – Jesper Juhl
    Apr 16 at 12:42






  • 1





    @Artyer Done well, the call to the lambda can be inlined.

    – Angew
    Apr 16 at 12:43

















10















The following function will randomly "sprinkle salt" on a loaded image. For the sake of boosting performance, the conditional statement



uint j = rows == 1 ? 0 : randomRow(generator);


should not be inside the loop.



Instead, I want to define a lambda getJ before the loop as



auto getJ = rows == 1 ? []() return 0; : []() return randomRow(generator); ;


However, my code with this lambda does not compile with the following red squiggled text:



enter image description here



Question



How to conditionally define such a lambda?



void salt_(Mat mat, unsigned long long n)

const uchar channels = mat.channels();
uint cols = mat.cols;
uint rows = mat.rows;

if (mat.isContinuous())

cols *= rows;
rows = 1;


default_random_engine generator;
uniform_int_distribution<uint> randomRow(0, rows - 1);
uniform_int_distribution<uint> randomCol(0, cols - 1);



// auto getJ = rows == 1 ? []() return 0; : []() return randomRow(generator); ;


uchar * const data = mat.data;

for (unsigned long long counter = 0; counter < n; counter++)

uint i = randomCol(generator);
uint j = rows == 1 ? 0 : randomRow(generator);
//uint j = getJ();

uint index = channels * (cols * j + i);
for (uchar k = 0; k < channels; k++)
data[index + k] = 255;











share|improve this question



















  • 3





    "my code with this lambda does not compile" - What is the error you get?

    – Suma
    Apr 16 at 12:35











  • You are trying to reference local variable generator without capturing it. If second lambda was capture-free it would compile.

    – VTT
    Apr 16 at 12:36







  • 5





    Seems like a premature optimisation. Surely function call overhead would be greater than ternary overhead?

    – Artyer
    Apr 16 at 12:40






  • 2





    Is a conditional really a performance issue here? Have you profiled the code?

    – Jesper Juhl
    Apr 16 at 12:42






  • 1





    @Artyer Done well, the call to the lambda can be inlined.

    – Angew
    Apr 16 at 12:43













10












10








10


1






The following function will randomly "sprinkle salt" on a loaded image. For the sake of boosting performance, the conditional statement



uint j = rows == 1 ? 0 : randomRow(generator);


should not be inside the loop.



Instead, I want to define a lambda getJ before the loop as



auto getJ = rows == 1 ? []() return 0; : []() return randomRow(generator); ;


However, my code with this lambda does not compile with the following red squiggled text:



enter image description here



Question



How to conditionally define such a lambda?



void salt_(Mat mat, unsigned long long n)

const uchar channels = mat.channels();
uint cols = mat.cols;
uint rows = mat.rows;

if (mat.isContinuous())

cols *= rows;
rows = 1;


default_random_engine generator;
uniform_int_distribution<uint> randomRow(0, rows - 1);
uniform_int_distribution<uint> randomCol(0, cols - 1);



// auto getJ = rows == 1 ? []() return 0; : []() return randomRow(generator); ;


uchar * const data = mat.data;

for (unsigned long long counter = 0; counter < n; counter++)

uint i = randomCol(generator);
uint j = rows == 1 ? 0 : randomRow(generator);
//uint j = getJ();

uint index = channels * (cols * j + i);
for (uchar k = 0; k < channels; k++)
data[index + k] = 255;











share|improve this question
















The following function will randomly "sprinkle salt" on a loaded image. For the sake of boosting performance, the conditional statement



uint j = rows == 1 ? 0 : randomRow(generator);


should not be inside the loop.



Instead, I want to define a lambda getJ before the loop as



auto getJ = rows == 1 ? []() return 0; : []() return randomRow(generator); ;


However, my code with this lambda does not compile with the following red squiggled text:



enter image description here



Question



How to conditionally define such a lambda?



void salt_(Mat mat, unsigned long long n)

const uchar channels = mat.channels();
uint cols = mat.cols;
uint rows = mat.rows;

if (mat.isContinuous())

cols *= rows;
rows = 1;


default_random_engine generator;
uniform_int_distribution<uint> randomRow(0, rows - 1);
uniform_int_distribution<uint> randomCol(0, cols - 1);



// auto getJ = rows == 1 ? []() return 0; : []() return randomRow(generator); ;


uchar * const data = mat.data;

for (unsigned long long counter = 0; counter < n; counter++)

uint i = randomCol(generator);
uint j = rows == 1 ? 0 : randomRow(generator);
//uint j = getJ();

uint index = channels * (cols * j + i);
for (uchar k = 0; k < channels; k++)
data[index + k] = 255;








c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 16 at 12:42







Artificial Hairless Armpit

















asked Apr 16 at 12:32









Artificial Hairless ArmpitArtificial Hairless Armpit

1,3441536




1,3441536







  • 3





    "my code with this lambda does not compile" - What is the error you get?

    – Suma
    Apr 16 at 12:35











  • You are trying to reference local variable generator without capturing it. If second lambda was capture-free it would compile.

    – VTT
    Apr 16 at 12:36







  • 5





    Seems like a premature optimisation. Surely function call overhead would be greater than ternary overhead?

    – Artyer
    Apr 16 at 12:40






  • 2





    Is a conditional really a performance issue here? Have you profiled the code?

    – Jesper Juhl
    Apr 16 at 12:42






  • 1





    @Artyer Done well, the call to the lambda can be inlined.

    – Angew
    Apr 16 at 12:43












  • 3





    "my code with this lambda does not compile" - What is the error you get?

    – Suma
    Apr 16 at 12:35











  • You are trying to reference local variable generator without capturing it. If second lambda was capture-free it would compile.

    – VTT
    Apr 16 at 12:36







  • 5





    Seems like a premature optimisation. Surely function call overhead would be greater than ternary overhead?

    – Artyer
    Apr 16 at 12:40






  • 2





    Is a conditional really a performance issue here? Have you profiled the code?

    – Jesper Juhl
    Apr 16 at 12:42






  • 1





    @Artyer Done well, the call to the lambda can be inlined.

    – Angew
    Apr 16 at 12:43







3




3





"my code with this lambda does not compile" - What is the error you get?

– Suma
Apr 16 at 12:35





"my code with this lambda does not compile" - What is the error you get?

– Suma
Apr 16 at 12:35













You are trying to reference local variable generator without capturing it. If second lambda was capture-free it would compile.

– VTT
Apr 16 at 12:36






You are trying to reference local variable generator without capturing it. If second lambda was capture-free it would compile.

– VTT
Apr 16 at 12:36





5




5





Seems like a premature optimisation. Surely function call overhead would be greater than ternary overhead?

– Artyer
Apr 16 at 12:40





Seems like a premature optimisation. Surely function call overhead would be greater than ternary overhead?

– Artyer
Apr 16 at 12:40




2




2





Is a conditional really a performance issue here? Have you profiled the code?

– Jesper Juhl
Apr 16 at 12:42





Is a conditional really a performance issue here? Have you profiled the code?

– Jesper Juhl
Apr 16 at 12:42




1




1





@Artyer Done well, the call to the lambda can be inlined.

– Angew
Apr 16 at 12:43





@Artyer Done well, the call to the lambda can be inlined.

– Angew
Apr 16 at 12:43












2 Answers
2






active

oldest

votes


















10















my code with this lambda does not compile with the following red squiggled text




You cannot use randomRow inside the body of the lambda expression without capturing it beforehand, as the generated closure object needs to have access to it.



Even if you were to use [&randomRow], the code would still fail to compile as every lambda expression produces a closure of unique type, even if the lambda expressions are exactly the same.



You can turn the problem on its head to avoid any overhead and achieve what you want - create a function that takes the lambda you want to invoke:



template <typename F>
void saltImpl(F&& getJ, /* ... */)

uchar * const data = mat.data;

for (unsigned long long counter = 0; counter < n; counter++)

uint i = randomCol(generator);
uint j = rows == 1 ? 0 : randomRow(generator);
//uint j = getJ();

uint index = channels * (cols * j + i);
for (uchar k = 0; k < channels; k++)
data[index + k] = 255;




Usage example:



void salt_(Mat mat, unsigned long long n)

const uchar channels = mat.channels();
uint cols = mat.cols;
uint rows = mat.rows;

if (mat.isContinuous())

cols *= rows;
rows = 1;


default_random_engine generator;
uniform_int_distribution<uint> randomRow(0, rows - 1);
uniform_int_distribution<uint> randomCol(0, cols - 1);

if (rows == 1)

saltImpl([] return 0; , /* ... */);

else

saltImpl([&] return randomRow(generator); , /* ... */)







share|improve this answer




















  • 1





    Why not write a lambda that takes a lambda instead?

    – Yakk - Adam Nevraumont
    Apr 16 at 13:41


















3














Why this fails is because the lambdas are of a different type. That's natural, their operator() have different definitions. Which means you want your following code to work with two different types. And the C++ way of making code work with different types is using templates.



Convert the code using getJ to a function template (it can be local to your implementation file), like this:



template <class G>
void salt_impl_(Mat mat, unsigned long long n, default_random_engine &generator, G getJ)

const uchar channels = mat.channels();
uint cols = mat.cols;
uint rows = mat.rows;

if (mat.isContinuous())

cols *= rows;
rows = 1;


uchar * const data = mat.data;

uniform_int_distribution<uint> randomCol(0, cols - 1);

for (unsigned long long counter = 0; counter < n; counter++)

uint i = randomCol(generator);
uint j = getJ();

uint index = channels * (cols * j + i);
for (uchar k = 0; k < channels; k++)
data[index + k] = 255;




void salt_(Mat mat, unsigned long long n)

const uchar channels = mat.channels();
uint cols = mat.cols;
uint rows = mat.rows;

if (mat.isContinuous())

cols *= rows;
rows = 1;


default_random_engine generator;
uniform_int_distribution<uint> randomRow(0, rows - 1);

if (rows == 1)
salt_impl_(mat, n, generator, []() return 0; );
else
salt_impl_(mat, n, generator, [&]() return randomRow(generator); );



Feel free to reduce the initial-part duplication between the function and the template by passing more parameters, making them members of a class, or something similar.



Also note that the non-trivial lambda must capture the variables which it accesses (randomRow and generator). I did this using the universal by-reference capture [&] in the code above.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55708242%2fhow-to-conditionally-define-a-lambda%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    10















    my code with this lambda does not compile with the following red squiggled text




    You cannot use randomRow inside the body of the lambda expression without capturing it beforehand, as the generated closure object needs to have access to it.



    Even if you were to use [&randomRow], the code would still fail to compile as every lambda expression produces a closure of unique type, even if the lambda expressions are exactly the same.



    You can turn the problem on its head to avoid any overhead and achieve what you want - create a function that takes the lambda you want to invoke:



    template <typename F>
    void saltImpl(F&& getJ, /* ... */)

    uchar * const data = mat.data;

    for (unsigned long long counter = 0; counter < n; counter++)

    uint i = randomCol(generator);
    uint j = rows == 1 ? 0 : randomRow(generator);
    //uint j = getJ();

    uint index = channels * (cols * j + i);
    for (uchar k = 0; k < channels; k++)
    data[index + k] = 255;




    Usage example:



    void salt_(Mat mat, unsigned long long n)

    const uchar channels = mat.channels();
    uint cols = mat.cols;
    uint rows = mat.rows;

    if (mat.isContinuous())

    cols *= rows;
    rows = 1;


    default_random_engine generator;
    uniform_int_distribution<uint> randomRow(0, rows - 1);
    uniform_int_distribution<uint> randomCol(0, cols - 1);

    if (rows == 1)

    saltImpl([] return 0; , /* ... */);

    else

    saltImpl([&] return randomRow(generator); , /* ... */)







    share|improve this answer




















    • 1





      Why not write a lambda that takes a lambda instead?

      – Yakk - Adam Nevraumont
      Apr 16 at 13:41















    10















    my code with this lambda does not compile with the following red squiggled text




    You cannot use randomRow inside the body of the lambda expression without capturing it beforehand, as the generated closure object needs to have access to it.



    Even if you were to use [&randomRow], the code would still fail to compile as every lambda expression produces a closure of unique type, even if the lambda expressions are exactly the same.



    You can turn the problem on its head to avoid any overhead and achieve what you want - create a function that takes the lambda you want to invoke:



    template <typename F>
    void saltImpl(F&& getJ, /* ... */)

    uchar * const data = mat.data;

    for (unsigned long long counter = 0; counter < n; counter++)

    uint i = randomCol(generator);
    uint j = rows == 1 ? 0 : randomRow(generator);
    //uint j = getJ();

    uint index = channels * (cols * j + i);
    for (uchar k = 0; k < channels; k++)
    data[index + k] = 255;




    Usage example:



    void salt_(Mat mat, unsigned long long n)

    const uchar channels = mat.channels();
    uint cols = mat.cols;
    uint rows = mat.rows;

    if (mat.isContinuous())

    cols *= rows;
    rows = 1;


    default_random_engine generator;
    uniform_int_distribution<uint> randomRow(0, rows - 1);
    uniform_int_distribution<uint> randomCol(0, cols - 1);

    if (rows == 1)

    saltImpl([] return 0; , /* ... */);

    else

    saltImpl([&] return randomRow(generator); , /* ... */)







    share|improve this answer




















    • 1





      Why not write a lambda that takes a lambda instead?

      – Yakk - Adam Nevraumont
      Apr 16 at 13:41













    10












    10








    10








    my code with this lambda does not compile with the following red squiggled text




    You cannot use randomRow inside the body of the lambda expression without capturing it beforehand, as the generated closure object needs to have access to it.



    Even if you were to use [&randomRow], the code would still fail to compile as every lambda expression produces a closure of unique type, even if the lambda expressions are exactly the same.



    You can turn the problem on its head to avoid any overhead and achieve what you want - create a function that takes the lambda you want to invoke:



    template <typename F>
    void saltImpl(F&& getJ, /* ... */)

    uchar * const data = mat.data;

    for (unsigned long long counter = 0; counter < n; counter++)

    uint i = randomCol(generator);
    uint j = rows == 1 ? 0 : randomRow(generator);
    //uint j = getJ();

    uint index = channels * (cols * j + i);
    for (uchar k = 0; k < channels; k++)
    data[index + k] = 255;




    Usage example:



    void salt_(Mat mat, unsigned long long n)

    const uchar channels = mat.channels();
    uint cols = mat.cols;
    uint rows = mat.rows;

    if (mat.isContinuous())

    cols *= rows;
    rows = 1;


    default_random_engine generator;
    uniform_int_distribution<uint> randomRow(0, rows - 1);
    uniform_int_distribution<uint> randomCol(0, cols - 1);

    if (rows == 1)

    saltImpl([] return 0; , /* ... */);

    else

    saltImpl([&] return randomRow(generator); , /* ... */)







    share|improve this answer
















    my code with this lambda does not compile with the following red squiggled text




    You cannot use randomRow inside the body of the lambda expression without capturing it beforehand, as the generated closure object needs to have access to it.



    Even if you were to use [&randomRow], the code would still fail to compile as every lambda expression produces a closure of unique type, even if the lambda expressions are exactly the same.



    You can turn the problem on its head to avoid any overhead and achieve what you want - create a function that takes the lambda you want to invoke:



    template <typename F>
    void saltImpl(F&& getJ, /* ... */)

    uchar * const data = mat.data;

    for (unsigned long long counter = 0; counter < n; counter++)

    uint i = randomCol(generator);
    uint j = rows == 1 ? 0 : randomRow(generator);
    //uint j = getJ();

    uint index = channels * (cols * j + i);
    for (uchar k = 0; k < channels; k++)
    data[index + k] = 255;




    Usage example:



    void salt_(Mat mat, unsigned long long n)

    const uchar channels = mat.channels();
    uint cols = mat.cols;
    uint rows = mat.rows;

    if (mat.isContinuous())

    cols *= rows;
    rows = 1;


    default_random_engine generator;
    uniform_int_distribution<uint> randomRow(0, rows - 1);
    uniform_int_distribution<uint> randomCol(0, cols - 1);

    if (rows == 1)

    saltImpl([] return 0; , /* ... */);

    else

    saltImpl([&] return randomRow(generator); , /* ... */)








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 16 at 12:49

























    answered Apr 16 at 12:42









    Vittorio RomeoVittorio Romeo

    59.8k17166310




    59.8k17166310







    • 1





      Why not write a lambda that takes a lambda instead?

      – Yakk - Adam Nevraumont
      Apr 16 at 13:41












    • 1





      Why not write a lambda that takes a lambda instead?

      – Yakk - Adam Nevraumont
      Apr 16 at 13:41







    1




    1





    Why not write a lambda that takes a lambda instead?

    – Yakk - Adam Nevraumont
    Apr 16 at 13:41





    Why not write a lambda that takes a lambda instead?

    – Yakk - Adam Nevraumont
    Apr 16 at 13:41













    3














    Why this fails is because the lambdas are of a different type. That's natural, their operator() have different definitions. Which means you want your following code to work with two different types. And the C++ way of making code work with different types is using templates.



    Convert the code using getJ to a function template (it can be local to your implementation file), like this:



    template <class G>
    void salt_impl_(Mat mat, unsigned long long n, default_random_engine &generator, G getJ)

    const uchar channels = mat.channels();
    uint cols = mat.cols;
    uint rows = mat.rows;

    if (mat.isContinuous())

    cols *= rows;
    rows = 1;


    uchar * const data = mat.data;

    uniform_int_distribution<uint> randomCol(0, cols - 1);

    for (unsigned long long counter = 0; counter < n; counter++)

    uint i = randomCol(generator);
    uint j = getJ();

    uint index = channels * (cols * j + i);
    for (uchar k = 0; k < channels; k++)
    data[index + k] = 255;




    void salt_(Mat mat, unsigned long long n)

    const uchar channels = mat.channels();
    uint cols = mat.cols;
    uint rows = mat.rows;

    if (mat.isContinuous())

    cols *= rows;
    rows = 1;


    default_random_engine generator;
    uniform_int_distribution<uint> randomRow(0, rows - 1);

    if (rows == 1)
    salt_impl_(mat, n, generator, []() return 0; );
    else
    salt_impl_(mat, n, generator, [&]() return randomRow(generator); );



    Feel free to reduce the initial-part duplication between the function and the template by passing more parameters, making them members of a class, or something similar.



    Also note that the non-trivial lambda must capture the variables which it accesses (randomRow and generator). I did this using the universal by-reference capture [&] in the code above.






    share|improve this answer



























      3














      Why this fails is because the lambdas are of a different type. That's natural, their operator() have different definitions. Which means you want your following code to work with two different types. And the C++ way of making code work with different types is using templates.



      Convert the code using getJ to a function template (it can be local to your implementation file), like this:



      template <class G>
      void salt_impl_(Mat mat, unsigned long long n, default_random_engine &generator, G getJ)

      const uchar channels = mat.channels();
      uint cols = mat.cols;
      uint rows = mat.rows;

      if (mat.isContinuous())

      cols *= rows;
      rows = 1;


      uchar * const data = mat.data;

      uniform_int_distribution<uint> randomCol(0, cols - 1);

      for (unsigned long long counter = 0; counter < n; counter++)

      uint i = randomCol(generator);
      uint j = getJ();

      uint index = channels * (cols * j + i);
      for (uchar k = 0; k < channels; k++)
      data[index + k] = 255;




      void salt_(Mat mat, unsigned long long n)

      const uchar channels = mat.channels();
      uint cols = mat.cols;
      uint rows = mat.rows;

      if (mat.isContinuous())

      cols *= rows;
      rows = 1;


      default_random_engine generator;
      uniform_int_distribution<uint> randomRow(0, rows - 1);

      if (rows == 1)
      salt_impl_(mat, n, generator, []() return 0; );
      else
      salt_impl_(mat, n, generator, [&]() return randomRow(generator); );



      Feel free to reduce the initial-part duplication between the function and the template by passing more parameters, making them members of a class, or something similar.



      Also note that the non-trivial lambda must capture the variables which it accesses (randomRow and generator). I did this using the universal by-reference capture [&] in the code above.






      share|improve this answer

























        3












        3








        3







        Why this fails is because the lambdas are of a different type. That's natural, their operator() have different definitions. Which means you want your following code to work with two different types. And the C++ way of making code work with different types is using templates.



        Convert the code using getJ to a function template (it can be local to your implementation file), like this:



        template <class G>
        void salt_impl_(Mat mat, unsigned long long n, default_random_engine &generator, G getJ)

        const uchar channels = mat.channels();
        uint cols = mat.cols;
        uint rows = mat.rows;

        if (mat.isContinuous())

        cols *= rows;
        rows = 1;


        uchar * const data = mat.data;

        uniform_int_distribution<uint> randomCol(0, cols - 1);

        for (unsigned long long counter = 0; counter < n; counter++)

        uint i = randomCol(generator);
        uint j = getJ();

        uint index = channels * (cols * j + i);
        for (uchar k = 0; k < channels; k++)
        data[index + k] = 255;




        void salt_(Mat mat, unsigned long long n)

        const uchar channels = mat.channels();
        uint cols = mat.cols;
        uint rows = mat.rows;

        if (mat.isContinuous())

        cols *= rows;
        rows = 1;


        default_random_engine generator;
        uniform_int_distribution<uint> randomRow(0, rows - 1);

        if (rows == 1)
        salt_impl_(mat, n, generator, []() return 0; );
        else
        salt_impl_(mat, n, generator, [&]() return randomRow(generator); );



        Feel free to reduce the initial-part duplication between the function and the template by passing more parameters, making them members of a class, or something similar.



        Also note that the non-trivial lambda must capture the variables which it accesses (randomRow and generator). I did this using the universal by-reference capture [&] in the code above.






        share|improve this answer













        Why this fails is because the lambdas are of a different type. That's natural, their operator() have different definitions. Which means you want your following code to work with two different types. And the C++ way of making code work with different types is using templates.



        Convert the code using getJ to a function template (it can be local to your implementation file), like this:



        template <class G>
        void salt_impl_(Mat mat, unsigned long long n, default_random_engine &generator, G getJ)

        const uchar channels = mat.channels();
        uint cols = mat.cols;
        uint rows = mat.rows;

        if (mat.isContinuous())

        cols *= rows;
        rows = 1;


        uchar * const data = mat.data;

        uniform_int_distribution<uint> randomCol(0, cols - 1);

        for (unsigned long long counter = 0; counter < n; counter++)

        uint i = randomCol(generator);
        uint j = getJ();

        uint index = channels * (cols * j + i);
        for (uchar k = 0; k < channels; k++)
        data[index + k] = 255;




        void salt_(Mat mat, unsigned long long n)

        const uchar channels = mat.channels();
        uint cols = mat.cols;
        uint rows = mat.rows;

        if (mat.isContinuous())

        cols *= rows;
        rows = 1;


        default_random_engine generator;
        uniform_int_distribution<uint> randomRow(0, rows - 1);

        if (rows == 1)
        salt_impl_(mat, n, generator, []() return 0; );
        else
        salt_impl_(mat, n, generator, [&]() return randomRow(generator); );



        Feel free to reduce the initial-part duplication between the function and the template by passing more parameters, making them members of a class, or something similar.



        Also note that the non-trivial lambda must capture the variables which it accesses (randomRow and generator). I did this using the universal by-reference capture [&] in the code above.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 16 at 12:42









        AngewAngew

        135k11261355




        135k11261355



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55708242%2fhow-to-conditionally-define-a-lambda%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

            Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

            Vilaño, A Laracha Índice Patrimonio | Lugares e parroquias | Véxase tamén | Menú de navegación43°14′52″N 8°36′03″O / 43.24775, -8.60070