std::make_pair
See original GitHub issueCython does not include definitions for std::make_pair
The blog post https://molpopgen.github.io/fwdpy/docs/examples/cython/WritingExtensions includes the following example
%%cython --cplus --compile-args=-std=c++11 -I $fwdpy_includes -I $fwdpp_includes -l sequence -l gsl -l gslcblas
from fwdpy.fwdpy cimport *
from libcpp.utility cimport pair
import numpy as np
ctypedef vector[double] vd
ctypedef pair[vd,vd] pvdvd
#Annoyingly, Cython currently does not
#expose std::make_pair, so we will
#do it here ourselves!
#If we don't have std::make_pair,
#we end up making extra temporary copies
#of our return values in memory. That's
#not cool, as we're doing this because
#we care about efficiency!
cdef extern from "<utility>" namespace "std" nogil:
pair[T,U] make_pair[T,U](T&,U&)
cdef pvdvd freq_esize_cpp(const singlepop_t * pop):
cdef vd freq,esize
cdef double twoN = 2.0*float(pop.N)
cdef size_t i = 0
cdef size_t nm=0
for i in range(pop.mcounts.size()):
if pop.mcounts[i]>0:
if pop.mutations[i].neutral is False:
freq.push_back(float(pop.mcounts[i])/twoN)
#s records the effect size/selection coefficient
esize.push_back(pop.mutations[i].s)
#For some reason, we need to provide casts
#so that Cython can get the types right for the call to
#make_pair:
return make_pair(<vd>freq,<vd>esize)
def freq_esize(Spop pop):
return freq_esize_cpp(pop.pop.get())
Is this the “best practice” way? If so, would it make sense to include the make_pair definition in Includes/libcpp/
?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (5 by maintainers)
Top Results From Across the Web
std::make_pair - cppreference.com
Creates a std::pair object, deducing the target type from the types of arguments. The deduced types V1 and V2 are std::decay<T1> ...
Read more >std::make_pair - CPlusPlus.com
Constructs a pair object with its first element set to x and its second element set to y . The template types can...
Read more >make_pair vs the constructor of std::pair? - Stack Overflow
The difference is that with std::pair you need to specify the types of both elements, whereas std::make_pair will create a pair with the ......
Read more >std::make_pair - cppreference.com
Creates a std::pair object, deducing the target type from the types of arguments. The deduced types V1 and V2 are std::decay<T1> ...
Read more >C++ Utility Library - make_pair Function - Tutorialspoint
C++ Utility Library - make_pair Function ; Return Value. It returns a pair object whose elements first and second are set to x...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, for sure. PRs welcome.
On Fri, Nov 9, 2018, 5:17 AM Leonard Lausen <notifications@github.com wrote:
Fixed by changing
make_pair( 1, 2 )
topair[ int, int ]( 1, 2 )