user92382
user92382

Reputation: 389

GCC function multiversioning and namespaces

Consider the following code snippet which uses GCC's function multiversioning.

//ver.h
#include<string>
namespace nt {
__attribute__ ((target ("default"))) std::string version();
__attribute__ ((target ("sse2")))    std::string version();
__attribute__ ((target ("ssse3")))   std::string version();
__attribute__ ((target ("sse4")))    std::string version();
}

//ver.cpp   
#include "ver.h"
using namespace nt;    
__attribute__ ((target ("default"))) std::string nt::version() { return "default"; } 
__attribute__ ((target ("sse2")))    std::string nt::version() { return "sse2";    } 
__attribute__ ((target ("ssse3")))   std::string nt::version() { return "ssse3";   } 
__attribute__ ((target ("sse4")))    std::string nt::version() { return "sse4";    }

The code works fine if the functions are in the global namespace. However, when they are wrapped inside a namespace, the compilation fails with

error: missing ‘target’ attribute for multi-versioned std::string nt::version()

I'm using GCC 4.8.2. Any help is appreciated.

Upvotes: 2

Views: 782

Answers (1)

nos
nos

Reputation: 229264

You need to define the functions in the nt namespace too.

//ver.cpp   
#include "ver.h"
namespace nt {
  __attribute__ ((target ("default"))) std::string version() { return "default"; } 
  __attribute__ ((target ("sse2")))    std::string version() { return "sse2";    } 
  __attribute__ ((target ("ssse3")))   std::string version() { return "ssse3";   } 
  __attribute__ ((target ("sse4")))    std::string version() { return "sse4";    }
}

Upvotes: 1

Related Questions