Feng Wang
Feng Wang

Reputation: 1594

why nvcc is not happy with functor

code of hc.cu:

//patch for g++4.7
#ifdef _GLIBCXX_ATOMIC_BUILTINS
#undef _GLIBCXX_ATOMIC_BUILTINS
#endif
#ifdef _GLIBCXX_USE_INT128
#undef _GLIBCXX_USE_INT128
#endif

struct pi_
{
    double operator()() const
    {
        return 3.141592653589793;
    }
};

#include <iostream>

int main()
{
    std::cout << pi_()();

    return 0;
}

compilation will result in two(not one) errors:

$nvcc -c hc.cu
hc.cu: In function ‘int main()’:
hc.cu:21:22: error: ‘type name’ declared as function returning a function
hc.cu:21:22: error: ‘type name’ declared as function returning a function

can someone please tell me the problem? I am an archlinux user using g++ 4.7, and I have to undefine two macros to make nvcc work; does these two undefs play some role in the compilation errors?

some infomation about cuda:

$ pacman -Qi cuda
Name           : cuda
Version        : 5.0.35-3
URL            : http://www.nvidia.com/object/cuda_home.html
Licenses       : custom
Groups         : None
Provides       : cuda-toolkit  cuda-sdk
Depends On     : gcc-libs  opencl-nvidia
Optional Deps  : gdb: for cuda-gdb
Required By    : cuda-z  cula  cusp
Conflicts With : None
Replaces       : cuda-toolkit  cuda-sdk
Installed Size : 1464880.00 KiB
Packager       : Sven-Hendrik Haase <[email protected]>
Architecture   : x86_64
Build Date     : Tue 30 Oct 2012 12:51:49 PM CET
Install Date   : Wed 09 Jan 2013 02:38:26 PM CET
Install Reason : Explicitly installed
Install Script : Yes
Description    : NVIDIA's GPU programming toolkit

and g++

pacman -Qi gcc-multilib
Name           : gcc-multilib
Version        : 4.7.2-3
URL            : http://gcc.gnu.org
Licenses       : GPL  LGPL  FDL  custom
Groups         : multilib-devel
Provides       : gcc=4.7.2-3
Depends On     : gcc-libs-multilib=4.7.2-3  binutils-multilib>=2.23      libmpc      cloog  ppl
Optional Deps  : None
Required By    : boost-build  chicken  clang  dkms  gcc-fortran-multilib     gcc-objc-multilib  ghc  htmldoc  libreoffice-sdk  libtool  virtualbox-host-dkms
Conflicts With : gcc
Replaces       : None
Installed Size : 81560.00 KiB
Packager       : Jan Alexander Steffens (heftig) <[email protected]>
Architecture   : x86_64
Build Date     : Wed 26 Dec 2012 01:22:52 PM CET
Install Date   : Mon 31 Dec 2012 03:40:26 PM CET
Install Reason : Installed as a dependency for another package
Install Script : Yes
Description    : The GNU Compiler Collection - C and C++ frontends for multilib

Upvotes: 1

Views: 617

Answers (1)

kangshiyin
kangshiyin

Reputation: 9779

I can repeat your error with my nvcc in CUDA 5.0.

It looks like a bug of nvcc.

pi_()() works in g++, but not in nvcc.

To make it work in nvcc, you have to write like this.

pi_ p;
std::cout<<p()<<std::endl;

I get this error when using (pi_())() in nvcc. It seems nvcc mistakenly treats pi_ as type cast.

a.cu(14): error: cast to type "pi_ ()" is not allowed

a.cu(14): error: expected an expression

2 errors detected in the compilation of "/tmp/tmpxft_00003006_00000000-6_a.cpp1.ii".

Upvotes: 3

Related Questions